Compare commits

...

93 Commits

Author SHA1 Message Date
review
6c589ff8bc docs(im): make the reactions pagination pointer actionable
The eval showed an agent guessing a conventional page size instead of
consulting schema. The file did carry a schema pointer, but a generic one
-- "run schema for the authoritative parameters" does not tell the agent
which field to look up, so it never looked.

Name the two fields (queries[].page_token, page_size_per_message), say
they live under --data, and bind them to the rule that a batch fragment is
not a complete reaction set. The values themselves stay in schema.
2026-07-31 18:03:50 +08:00
review
bcab2ba473 docs(im): drop scope mapping table from skill body 2026-07-31 18:03:50 +08:00
review
d4c59e3d3f docs(im): trim chat-search reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
7f76764bd6 docs(im): trim chat-list reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
35744837d5 docs(im): trim chat-messages-list reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
9e52ae912c docs(im): trim chat-create reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
7bf173ec59 docs(im): trim messages-search reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
bcd0c492f6 docs(im): trim reactions reference and mark emoji enum as help-gap 2026-07-31 18:03:50 +08:00
review
a8270bc89b docs(im): trim feed-groups reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
b7bc918807 docs(im): trim messages-reply reference to non-derivable content 2026-07-31 18:03:50 +08:00
review
8af92fb446 docs(im): trim messages-send reference to non-derivable content 2026-07-31 18:03:49 +08:00
luozhixiong
de3c7ba733 test(im): use approved fixture domain 2026-07-31 17:51:43 +08:00
luozhixiong
928a3ca826 chore(im): trim review-only artifacts 2026-07-31 17:29:52 +08:00
luozhixiong
bfdd978519 fix(im): remove superseded helpers 2026-07-31 17:11:06 +08:00
luozhixiong
ac8c577553 fix(im): refine identity and mention help signals 2026-07-31 16:56:14 +08:00
luozhixiong
b6c34959b3 fix(im): expose defaulted identity and align help layout 2026-07-31 16:56:14 +08:00
luozhixiong
cbca00425c fix(im): close completion contract review gaps 2026-07-31 16:56:14 +08:00
luozhixiong
79bae0e50e test(im): align acceptance contract coverage 2026-07-31 16:56:14 +08:00
luozhixiong
eba176e731 docs(im): state conservative mention evidence 2026-07-31 16:56:14 +08:00
luozhixiong
4e5dcbc46c fix(im): make agent guidance task-scoped 2026-07-31 16:56:14 +08:00
luozhixiong
89f88a0186 chore(im): align agent guidance and coverage 2026-07-31 16:56:14 +08:00
luozhixiong
96363db795 fix(im): close read completeness gaps 2026-07-31 16:56:14 +08:00
luozhixiong
f195175b7d feat(im): add structured message mentions 2026-07-31 16:56:14 +08:00
luozhixiong
db2d68714b feat(im): require idempotent chat creation
BREAKING CHANGE: im +chat-create now requires --idempotency-key.
2026-07-31 16:56:13 +08:00
luozhixiong
dd6e8ca06b fix(im): close completion contract gaps 2026-07-31 16:20:28 +08:00
luozhixiong
3d9bb902cc refactor(im): make acceptance-only a first-class contract 2026-07-31 16:20:28 +08:00
luozhixiong
b13305111e fix(im): keep contract exemptions public-safe 2026-07-31 16:20:28 +08:00
luozhixiong
fd6b8d3934 chore(im): align help skills and contract coverage 2026-07-31 16:20:28 +08:00
luozhixiong
d639a390de feat(im): expose trustworthy completeness for IM reads 2026-07-31 16:20:28 +08:00
luozhixiong
fc30060ad1 feat(im): enforce trustworthy completion for IM writes 2026-07-31 16:20:28 +08:00
luozhixiong
43444330d9 docs(im): pin bot identity on image pre-upload examples 2026-07-31 16:20:28 +08:00
luozhixiong
d57a86c8d5 docs(im): pin explicit identity on every outbound example command 2026-07-31 16:20:28 +08:00
luozhixiong
9ede9ae15d fix(im): pin explicit identity on user-only examples and run them verbatim 2026-07-31 16:20:28 +08:00
luozhixiong
73190643d3 test(im): use the scanner-recognized test-secret placeholder inline 2026-07-31 16:20:28 +08:00
luozhixiong
bcef5110da test(im): route config secret through printf placeholder for content scan 2026-07-31 16:20:28 +08:00
luozhixiong
b61ba28dea test(im): lock example identity, run all examples, guard affordance drift 2026-07-31 16:20:28 +08:00
luozhixiong
6288d7255f docs(im): extend approval semantics to all outbound actions and card drafts 2026-07-31 16:20:27 +08:00
luozhixiong
2bbbfc69c6 fix(im): fail closed on media upload errors instead of rewriting content 2026-07-31 16:20:27 +08:00
luozhixiong
43b75d2071 docs(im): mention the user-id direct message form for plain text sends 2026-07-31 16:20:27 +08:00
luozhixiong
2ddf2b4a1e test(im): drop redundant loop variable copy and type error assertions 2026-07-31 16:20:27 +08:00
luozhixiong
38a68a9f26 docs(im): forbid downgrading reply intent to a new direct message 2026-07-31 16:20:27 +08:00
luozhixiong
c12df3ec6a docs(im): cover reply target in domain approval rule and tighten hint lock 2026-07-31 16:20:27 +08:00
luozhixiong
b90c1a95f5 docs(im): surface sending approval semantics in the domain skill 2026-07-31 16:20:27 +08:00
luozhixiong
9c3dca2b34 docs(im): require draft approval when message content is delegated 2026-07-31 16:20:27 +08:00
luozhixiong
9279155db2 fix(im): add id-source hint and strengthen example and recovery locks 2026-07-31 16:20:27 +08:00
luozhixiong
5d61ecf492 feat(im): extend Tips examples to feed and flag shortcuts 2026-07-31 16:20:27 +08:00
luozhixiong
392c64e3a3 docs(im): relax send confirmation gate for fully specified requests 2026-07-31 16:20:27 +08:00
luozhixiong
4de60dd66a fix(im): seed failure inventory with recovery hint fix and coverage updates
Replay the seeded IM failure cases. Most already give an agent enough signal to recover; deterministic search coverage requires stable fixtures, while the --head/--tail conflict lacked a concrete next action. Add the missing hint, record the inventory, and fold the search fixture prerequisite and dry-run coverage into coverage.md.
2026-07-31 16:20:27 +08:00
luozhixiong
aa39825b03 feat(im): add raw API affordance guidance for the im domain 2026-07-31 16:20:27 +08:00
luozhixiong
7cc7fcf90c feat(im): add copyable Tips examples to high-frequency IM shortcuts 2026-07-31 16:20:27 +08:00
zhouyue-bytedance
5cf09ecfda docs(base): clarify form and file operation routing (#2110)
* docs(base): clarify form and file operation routing

* docs: clarify complete base role table rules

* docs: clarify base advanced permission status

* docs: clarify base form field lifecycle

* docs: guide base form question creation

* fix(base): address form dry-run review findings

* docs(base): add complete editable role example

* fix(base): validate form question create inputs
2026-07-31 15:23:03 +08:00
chenxingyang1019
41692b7041 feat(apps): add cache debug commands (+cache-get/-delete/-clear) (#1896)
* feat(apps): add cache debug commands (+cache-get/-delete/-clear)

Add three apps-domain cache debug shortcuts for inspecting/clearing an app's
runtime cache:
- +cache-get: read a business key's value + metadata (hit/miss)
- +cache-delete: delete a single key (idempotent, write)
- +cache-clear: clear all cache in an environment (high-risk-write, --yes)

value renders raw on --format json, deserialized on --format pretty;
value_size_bytes is computed CLI-side; --environment auto-selects the branch
when omitted. Includes unit tests (hit/miss/dry-run/confirmation) and the
lark-apps cache skill reference.

* fix(apps): normalize cache numeric output fields and tidy comments

Follow-up hardening for the cache debug commands (+cache-get/-delete/-clear):

- Normalize ttl_ms / deleted_key_count via a new cacheInt() helper so
  --format json emits a stable JSON number (or null) regardless of whether
  the server sends the value as a number or a string. Aligns with the
  repo convention that numeric wire fields may arrive as strings; previously
  these were passed through raw, leaving the output type at the server's mercy.
- Add unit tests locking the string-wire -> JSON number contract for both
  cache-get ttl_ms and cache-delete deleted_key_count.
- Tidy two comments: soften cacheBool's speculative "historical wire form"
  claim to a defensive-tolerance note, and drop implementation jargon from
  cache-delete's risk-level rationale.
2026-07-31 14:13:56 +08:00
dc-bytedance
b79827d60a fix: drop stale target version from root upgrade prompt (#2100) 2026-07-31 12:45:43 +08:00
zhaojiaxing-coding
0f35676a28 feat(drive): extend permission shortcuts for Miaoda (#2070)
* feat(drive): support Miaoda apps in permission shortcuts

Extend Drive permission shortcuts to accept Miaoda page URLs and the apps resource type while keeping each endpoint's accepted resource contract explicit.

Key features:

- Infer apps from /page/ URLs and accept explicit --type=apps in +apply-permission, +member-add, +member-list, and +permission-get-setting

- Decouple secure-label target parsing so expanding apply-permission does not widen secure-label support

- Align skill guidance and unit/dry-run coverage with the new resource type

* test(drive): cover apps permission target validation

Add focused coverage for Miaoda apps target handling across apply-permission and secure-label boundaries.

Exercise malformed page URLs, explicit apps bare tokens, typed validation errors, and command-level rejection so future resource-type changes cannot silently widen unsupported secure-label behavior.

* fix(drive): parse permission markers from URL paths

Keep drive +apply-permission resource inference aligned with URL component boundaries. Parse and validate URL inputs before extracting tokens so query strings and fragments cannot redirect permission requests to a different resource.

Key fixes:

- Match document and apps markers only against the parsed URL path

- Reject malformed URLs with a typed --token validation error

- Cover /page/ markers found only in query strings or fragments

* docs(skills): redact Miaoda page token example

Replace the concrete Miaoda page token with a representative pagcn placeholder. This keeps the token shape recognizable while avoiding exposure of a real resource identifier in the skill documentation.

* fix(drive): harden permission target resolution

Make Drive shortcut targets unambiguous before they reach read or write API paths. URL inputs now bind to a recognized root path and a single validated token segment, preventing encoded separators, dot segments, and type conflicts from silently changing the addressed resource.

Key fixes:

- Reject non-root URLs, dot/traversal tokens, and URL/type conflicts for secure-label and permission-apply writes

- Keep permission-setting URL parsing and pretty output reversible for every supported command-local resource kind

- Add unit and dry-run E2E regressions plus aligned permission-apply guidance
2026-07-31 12:16:22 +08:00
wangweiming-01
946964e093 fix(drive): use title for default download filename (#2089) 2026-07-31 12:12:11 +08:00
HanShaoshuai-k
cfe76ad56a ci: add protected public domain allowlists (#2111)
Co-authored-by: HanShaoshuai-k <268785735+HanShaoshuai-k@users.noreply.github.com>
2026-07-31 11:02:04 +08:00
calendar-assistant
fa9c30c690 docs(calendar): confirm scope before editing recurring events (#2119)
Promote the recurring-event rule to a pre-routing gate so it is read
before the specific operation flow, and require confirming the scope
(this event / all / this-and-following) when the user is ambiguous
instead of defaulting to this-event-only. Removes the redundant and
conflicting "edit existing event" row that hard-coded the single-
instance default.
2026-07-30 21:59:08 +08:00
zcc
ba95252019 feat(drive): add comment-operation shortcuts (#1898)
Add comment-domain shortcuts: +batch-query-comments, +resolve-comment,
+restore-comment, +add-reply, +list-replies, +update-reply, +delete-reply
and +react-reply, sharing one target resolver with per-endpoint file_type
sets.

Flatten the comment reference docs by dropping the comments-guide routing
layer and folding its cross-command knowledge into the command refs:
comment-card model, comment/reply/interaction counting and sorting rules
into lark-drive-list-comments.md; the --solved-status prerequisite into
lark-drive-restore-comment.md; the apps exception into
lark-drive-add-comment.md. Comment intents now route straight from the
drive SKILL.md Shortcuts table to each command ref.

Cover the new shortcuts with unit tests, dry-run e2e and live workflow
e2e behind LARK_DRIVE_MD_COMMENT_E2E=1, and register them in
tests/cli_e2e/drive/coverage.md.
2026-07-30 21:53:21 +08:00
zhouyue-bytedance
4a16139348 fix(base): resolve Base URL block types accurately (#2099)
* fix: resolve Base URL block types accurately

* fix: resolve Base block selection from Wiki URLs

* fix(base): guide resolved folder and docx blocks

* fix(base): avoid field fallback for untyped URL blocks

* docs(base): specify URL example fence language

* test(base): cover unmatched URL block resolution
2026-07-30 20:29:47 +08:00
BD-ZERO
6e5308af01 feat: add SXSD schema validation to Slides lint (#2103)
- add XSD-backed SXSD validation for tags, attributes, structure, scalar values, and namespaces
- preserve supported server-filled fields and readback namespace compatibility
- isolate SXSD failures by slide so valid slides continue through layout checks
- improve actionable lint diagnostics and suppress duplicate errors
- add regression coverage for schema validation and Slides readback cases

Validated with unit tests and real Slides create/readback round trips.
2026-07-30 20:04:53 +08:00
liangshuo-1
87be09ef5f fix(contact): stop bot match segments carrying tags or empty entries (#2115) 2026-07-30 18:08:38 +08:00
sang-neo03
a575a8ba60 feat(contact): add bot search shortcut (#2083) 2026-07-30 17:03:49 +08:00
calendar-assistant
1f565a290b docs(calendar): warn against container-default timezone in time conversion (#2104)
Agents dropping to the raw `calendar events create/patch` API must convert
wall-clock time to Unix timestamps themselves. In UTC containers this silently
yields an 8-hour offset. Require explicit ISO 8601 offsets on +create/+update
--start/--end, and warn that raw-API timestamp conversion must specify the
target timezone instead of relying on the container default.
2026-07-30 14:06:14 +08:00
yballul-bytedance
68a77eee5c feat: support visible_rule for form questions (#1891)
Form questions can now carry a visible_rule (display condition) so a question shows only when earlier questions match the rule. The rule shares the exact same structure as the view filter, so extract that structure into a single shared reference (lark-base-filter-condition.md) that both view-set-filter and visible_rule point to.

- create/update shortcuts: document visible_rule in --questions help and transcribe the questions body (including visible_rule) into dry-run output
- document that form question updates use full overwrite semantics and must preserve existing fields via read-modify-write
- skill refs: add visible_rule sections to form-questions create/update, note it is only needed when the user asks for a display condition, and clarify that the shared tuple filter protocol does not apply to data-query filters
- tests: pin flag help, verbatim visible_rule passthrough on create/update/list, and add dry-run E2E coverage

Co-authored-by: yballul-bytedance <273011618+yballul-bytedance@users.noreply.github.com>
Co-authored-by: TRAE CLI <noreply@bytedance.com>
2026-07-30 12:37:24 +08:00
liangshuo-1
29a97dbde8 chore: release v1.0.80 (#2101) 2026-07-29 21:37:15 +08:00
R0bynZhu
29a6a7b600 docs(slides): +create 的参数下沉到 create.md,主 skill 只留路由 (#2096)
* docs(slides): +create 的参数下沉到 create.md,主 skill 只留路由

trace 里 +create 的三类高频错误(--yes、--name、--slides 塞文件路径)
共同点是调用前没读 lark-slides-create.md。原因不是文档缺内容,而是
SKILL.md 里 +create 的信息「够又不够」:给了半截参数描述,模型觉得
够用就直接拼命令,不再打开文档。

- 删掉「创建方式选择」整节(表格 + 两条 WARNING),下沉到 create.md,
  由生成流程 Step 3 和核心规则 2 指向那份文档
- Shortcuts 表 +create 行、核心规则 2 不再复述参数
- Quick Reference 顶部说明参数以文档和 --help 为准,「新建 PPT」行补上
  create.md
- PPTX 一行改写为 drive +import 导入路径;create.md 里写明本命令不读
  本地文件
- create.md 增加「--slides 不接受的形态」对照表,并合并开头零散的
  禁止/推荐/最稳/注意条目
- @ 占位符统一写成 <img src="@./path">,消除「--slides 支持 @ 路径」的歧义

* docs(slides): 去掉 create.md 里的「--slides 不接受的形态」对照表

* docs(slides): 模板一行的触发条件补上「已有 PPTX 要改」

* docs(slides): create.md 澄清「不读取本地文件」的歧义

原句「本命令只从零创建演示文稿,不读取本地文件」与本文档
「本地图片:@<path> 占位符」一节自相矛盾——@ 占位符恰恰会读
本地图片并自动上传。改为只否定「导入本地 PPT 文件的参数」,
不波及图片占位符能力。

* docs(slides): 两步创建的第二步补上 slide create 文档路由

生成流程 Step 3 和「执行前必做」的创建一行原来只指向
lark-slides-create.md,而两步创建的第二步用的是
xml_presentation.slide create,文档没被路由到,模型只能凭
记忆拼参数。
2026-07-29 20:50:24 +08:00
liangshuo-1
c167163d70 feat: propagate invocation metadata (#2097) 2026-07-29 19:39:53 +08:00
zhaojiaxing-coding
7988515e1c feat(drive): add +permission-get-setting shortcut (#1738)
* feat(drive): add +permission-get-setting shortcut

Add a Drive shortcut for reading public permission settings across supported documents, files, folders, and wiki nodes. Resolve URLs into typed resources, preserve permission_public output for machine consumers, and document the shortcut in the permission-governance workflow.

Key features:

- Infer resource type and token from supported Drive URLs while requiring --type for bare tokens

- Query the Drive v2 public permission endpoint with typed validation and user or bot identity

- Support folder permission inspection without recursing into child resources

- Add unit, dry-run E2E, live workflow, output, and skill guidance coverage

* fix(drive): harden permission get setting contract

Harden +permission-get-setting after review findings so callers receive only the documented permission payload and folder support is verified against the live workflow. This prevents malformed responses from being presented as permission settings and keeps the command guidance aligned with the shortcut contract.

Key fixes:
- Reject responses without data.permission_public instead of projecting arbitrary payload fields
- Render complete permission settings in pretty output and mark --token required
- Exercise a created Drive folder in the live workflow and add the command reference
- Correct folder resolution guidance while retaining the shortcut's documented URL forms

* feat/drive-folder-permission-get
2026-07-29 17:57:24 +08:00
zhaojiaxing-coding
c7adff7a3b feat(drive): add +member-list shortcut (#1795)
* feat(drive): add +member-list shortcut

Add a Drive shortcut for listing collaborators on documents, files, folders, and wiki nodes. Resolve supported resource URLs into typed permission requests, preserve raw API data for machine consumers, and keep invalid flag combinations on typed validation paths.

Key features:

- Infer resource type and token from supported Drive URLs while requiring --type for bare tokens

- Validate optional member fields and wiki-only permission type filters

- Provide pretty output, skill guidance, unit coverage, and dry-run/live E2E workflows

- Read dry-run assertions from the standard data.api success envelope

* feat/drive-member-list
2026-07-29 17:04:59 +08:00
ethan-zhx
59237f3104 Feat/detect line text overlap (#2069)
* fix: report ghost text canvas overflow

* fix(slides): detect text-line overlap in xml_text_overlap_lint
2026-07-29 16:20:59 +08:00
R0bynZhu
358cd06838 docs(slides): 补齐 shortcut 参数说明,修正 +xml-get --output 必填标注 (#2088)
* docs(slides): consolidate CWD-relative path rule into one global rule

State the "all local file path args must be CWD-relative (absolute
rejected)" rule once in SKILL.md 权威经验, and trim the per-command
repetitions in media-upload / create / screenshot / xml-presentations-get.
Also fix the stale xml-presentations-get param table: --output is optional
(relative), not required.

* feat: try common solution

* chore: 优化措辞

* feat: 优化措辞

* feat: 优化措辞

* docs(slides): 强调调用命令前必读对应命令文档

- 「调用命令前再读」改为「调用相关命令前必须读取相关的文档以了解命令的使用方式」,
  并把原「按需再读」列表合并进来,去掉可选语义
- 移除 lark-shared 的 CRITICAL 前置阅读要求
- Step 4 回读示例补全 `--presentation <xml_presentation_id>` 参数

* docs(slides): Shortcuts 表补充 +screenshot 并写明本地路径参数

- 新增 +screenshot 行:--slide-number 页号(从 1 开始,可重复,一次最多 10 页)、
  --output-dir 保存目录(CWD 内相对路径,默认 .lark-slides/screenshots)
- +xml-get 行补上 --presentation 和 --output(CWD 内相对路径),
  并说明省略 --output 时 XML 返回在 JSON 信封里

* revert(slides): 回退 references 下的文档改动,只保留 SKILL.md

把 lark-slides-create.md、lark-slides-media-upload.md、lark-slides-screenshot.md、
lark-slides-xml-presentations-get.md 还原为 main 的版本,本分支只改 SKILL.md。

* docs(slides): 恢复开始前必读 lark-shared 的 CRITICAL 要求

认证、权限和全局参数以 lark-shared 为准,这条前置阅读不该在本分支被删掉。

* chore: 移除output省略的说明
2026-07-29 10:55:05 +08:00
Yuxuan Zhao
b0b1ca4b5d test(e2e): wait for base role update visibility (#2087) 2026-07-28 21:45:00 +08:00
liangshuo-1
781d188a60 chore: release v1.0.79 (#2082) 2026-07-28 21:02:37 +08:00
calendar-assistant
2e0fb9a880 docs(calendar): refine attendee guidance for bots and user-search identity (#2086)
Consolidate the user-search identity note into SKILL.md, and clarify bot
handling across attendee flows: bots are virtual identities with no
free/busy semantics, no meeting-room seat, and no room preference, so
they must be excluded from +suggestion, +room-find, and the scheduling
free/busy check. Note in create/update that bots remain valid attendees.
2026-07-28 20:34:09 +08:00
ILUO
927b37cd63 docs(task): document create data passthrough (#2080) 2026-07-28 20:26:35 +08:00
zhangjun-bytedance
d2e22c5fca feat: 0728 fix url (#2079) 2026-07-28 19:05:47 +08:00
ethan-zhx
fdae560014 docs(slides): add formula inline element syntax to quick-ref (#2077)
* docs(slides): add formula inline element syntax to quick-ref

* docs(slides): add chart gradient syntax to quick-ref
2026-07-28 17:40:54 +08:00
zhengzhijiej-tech
1b173e1953 fix(sheets): recognize OFL0X local office tokens (#2063) 2026-07-28 15:09:42 +08:00
ethan-zhx
57db1b3a8d feat(slides):update xsd (#2067) 2026-07-28 14:43:15 +08:00
calendar-assistant
4c1c5f5287 docs(calendar): clarify identity selection by event ownership (#2071)
Reframe the identity section around event ownership: use `--as user`
for the logged-in user's own events and `--as bot` for events the bot
creates or participates in, with matching `+agenda` examples.
2026-07-28 14:05:21 +08:00
liangshuo-1
3d2c10cd0b fix(ci): validate static workflow identity (#2015) 2026-07-27 19:39:11 +08:00
liangshuo-1
03de81c5f3 chore: release v1.0.78 (#2061) 2026-07-27 19:17:53 +08:00
yballul-bytedance
7abcaa7f68 feat(drive): add title+body joint search guidance and Top N pagination rules (#2059)
* feat(drive): add title+body joint search guidance and pagination rules for Top N results

- Add new blockquote explaining combined title+body search: use a single
  --query with both keywords instead of splitting into two searches
- Add rule for Top N results: N is an output cap, not --page-size; scan
  up to 3 pages filtering by title and summary_highlighted, read body
  only for title-matched candidates, stop early at N confirmed results
- Add quick-reference table row for folder-scoped title+body search
- Update pagination strategy rule to cover the 3-page cap for joint
  search in addition to the existing 5-page limit for other scenarios

* feat(drive): clarify Top N search output limit

* feat(drive): clarify search filters share one call

---------

Co-authored-by: yballul-bytedance <273011618+yballul-bytedance@users.noreply.github.com>
2026-07-27 17:19:48 +08:00
zhangjun-bytedance
8fb2476985 0727 fix rich text (#2062) 2026-07-27 16:17:08 +08:00
zhanghuanxu
56c9a2afd8 fix: exempt ghost text from slides lint 2026-07-27 11:59:04 +08:00
zhanghuanxu
2029189809 fix(slides):text may over flow shape 2026-07-27 11:59:04 +08:00
zhanghuanxu
ee427979a8 fix(slides): preserve info lint severity 2026-07-27 11:59:04 +08:00
zhanghuanxu
545abcbbde fix: refine character width estimation for lark-slides text lint
Replace the uniform 0.55em half-width coefficient with per-character-type
coefficients, add font-family awareness (sans/serif), bold multiplier,
letter-spacing support, and fix padding-aware line wrapping.

- Split half-width chars into uppercase (0.57), lowercase (0.51 sans / 0.53
  serif), digits (0.58), and punctuation (0.50)
- Add classify_font_family() to apply slightly wider lowercase widths for
  serif fonts (Georgia, Source Han Serif/思源宋体, Times, etc.)
- Add 5% width multiplier for bold text; detect <strong>/<b>/<i>/<em> tags
  and span-level bold/italic attributes in addition to content attrs
- Fix estimate_text_line_count_for_text to subtract paddingLeft/paddingRight
  from available width before computing wrap lines
- Add resolve_letter_spacing and wire letterSpacing through estimate_text_width
- Extract fontFamily/bold/italic/letterSpacing into element dict during parse
2026-07-27 11:59:04 +08:00
zhanghuanxu
4a73e83f1e fix(slides): allow chartParsedValues roundtrip tag
chartParsedValues is a server-injected roundtrip child tag under
chartField, not an attribute. Move it from ROUNDTRIP_SXSD_ATTRS to a
new ROUNDTRIP_SXSD_TAGS set and skip the tag (and its subtree) in the
SXSD tag whitelist check.
2026-07-27 11:59:04 +08:00
zhanghuanxu
7496420fa8 fix(slides): downgrade background-decoration text overflow to info
Large low-alpha text underneath other text shapes is typically a
background design element; treat text_may_overflow_shape as info in
that case instead of warning/error.
2026-07-27 11:59:04 +08:00
zhanghuanxu
43fabdf524 fix(slides): detect letterSpacing-driven text overflow
Extract letterSpacing from content/paragraph attrs and factor it into
width and line-count estimates, and stop short-circuiting the shape
overflow check for autoFit shapes so that letterSpacing-heavy captions
under normal-auto-fit no longer escape detection.
2026-07-27 11:59:04 +08:00
zhanghuanxu
8c46c74105 fix(slides): upgrade text overflow to error above 10px threshold
Text-shape overflow was always reported as a warning, which let clearly
broken pages pass the lint gate. Overflow > 10px now upgrades to error;
smaller overflows stay as warning to avoid flagging near-fit cases.
2026-07-27 11:59:04 +08:00
zhanghuanxu
70777c86c3 fix(slides): restrict canvas overflow checks 2026-07-27 11:59:04 +08:00
295 changed files with 35083 additions and 3930 deletions

3
.github/CODEOWNERS vendored
View File

@@ -1,4 +1,7 @@
/go.mod @liangshuo-1
/go.sum @liangshuo-1
/internal/ @liangshuo-1
/shortcuts/common/ @liangshuo-1
# Last match wins: existing domains below are exempt, only new skills/ entries need review.
/skills/ @liangshuo-1

View File

@@ -25,19 +25,16 @@ jobs:
with:
script: |
const run = context.payload.workflow_run;
if (run.name !== "CI") throw new Error(`unexpected workflow name: ${run.name}`);
let workflowPath = run.path || "";
if (!workflowPath) {
const workflowId = Number(run.workflow_id || 0);
if (!Number.isInteger(workflowId) || workflowId <= 0) throw new Error("missing workflow id");
const { data: workflow } = await github.rest.actions.getWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
});
workflowPath = workflow.path || "";
}
if (workflowPath !== ".github/workflows/ci.yml") throw new Error(`unexpected workflow path: ${workflowPath}`);
const workflowId = Number(run.workflow_id || 0);
if (!Number.isInteger(workflowId) || workflowId <= 0) throw new Error("missing workflow id");
const { data: workflow } = await github.rest.actions.getWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
});
if (workflow.name !== "CI") throw new Error(`unexpected workflow name: ${workflow.name}`);
if (workflow.path !== ".github/workflows/ci.yml") throw new Error(`unexpected workflow path: ${workflow.path}`);
if (run.path && run.path !== workflow.path) throw new Error(`workflow path mismatch: ${run.path}`);
if (run.event !== "pull_request") throw new Error(`unexpected event: ${run.event}`);
if (run.repository.id !== context.payload.repository.id) throw new Error("repository id mismatch");
if (run.repository.full_name !== context.payload.repository.full_name) throw new Error("repository name mismatch");
@@ -253,19 +250,16 @@ jobs:
with:
script: |
const run = context.payload.workflow_run;
if (run.name !== "CI") throw new Error(`unexpected workflow name: ${run.name}`);
let workflowPath = run.path || "";
if (!workflowPath) {
const workflowId = Number(run.workflow_id || 0);
if (!Number.isInteger(workflowId) || workflowId <= 0) throw new Error("missing workflow id");
const { data: workflow } = await github.rest.actions.getWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
});
workflowPath = workflow.path || "";
}
if (workflowPath !== ".github/workflows/ci.yml") throw new Error(`unexpected workflow path: ${workflowPath}`);
const workflowId = Number(run.workflow_id || 0);
if (!Number.isInteger(workflowId) || workflowId <= 0) throw new Error("missing workflow id");
const { data: workflow } = await github.rest.actions.getWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
});
if (workflow.name !== "CI") throw new Error(`unexpected workflow name: ${workflow.name}`);
if (workflow.path !== ".github/workflows/ci.yml") throw new Error(`unexpected workflow path: ${workflow.path}`);
if (run.path && run.path !== workflow.path) throw new Error(`workflow path mismatch: ${run.path}`);
if (run.event !== "pull_request") throw new Error(`unexpected event: ${run.event}`);
if (run.conclusion !== "success") throw new Error(`unexpected conclusion: ${run.conclusion}`);
if (run.repository.id !== context.payload.repository.id) throw new Error("repository id mismatch");

View File

@@ -2,6 +2,61 @@
All notable changes to this project will be documented in this file.
## [v1.0.80] - 2026-07-29
### Features
- **drive**: add +member-list shortcut (#1795)
- **drive**: add +permission-get-setting shortcut (#1738)
- propagate invocation metadata (#2097)
### Documentation
- **slides**: 补齐 shortcut 参数说明,修正 +xml-get --output 必填标注 (#2088)
- **slides**: +create 的参数下沉到 create.md主 skill 只留路由 (#2096)
### Tests
- **e2e**: wait for base role update visibility (#2087)
### Misc
- Feat/detect line text overlap (#2069)
## [v1.0.79] - 2026-07-28
### Features
- **slides**: update xsd (#2067)
### Bug Fixes
- **ci**: validate static workflow identity (#2015)
- **sheets**: recognize OFL0X local office tokens (#2063)
### Documentation
- **calendar**: clarify identity selection by event ownership (#2071)
- **slides**: add formula inline element syntax to quick-ref (#2077)
## [v1.0.78] - 2026-07-27
### Features
- event description support rich text (#1975)
### Bug Fixes
- **slides**: restrict canvas overflow checks
- **slides**: upgrade text overflow to error above 10px threshold
- **slides**: detect letterSpacing-driven text overflow
- **slides**: downgrade background-decoration text overflow to info
- **slides**: allow chartParsedValues roundtrip tag
- refine character width estimation for lark-slides text lint
- **slides**: preserve info lint severity
- **slides**: text may over flow shape
- exempt ghost text from slides lint
## [v1.0.77] - 2026-07-24
### Features
@@ -1667,6 +1722,9 @@ Bundled AI agent skills for intelligent assistance:
- Bilingual documentation (English & Chinese).
- CI/CD pipelines: linting, testing, coverage reporting, and automated releases.
[v1.0.80]: https://github.com/larksuite/cli/releases/tag/v1.0.80
[v1.0.79]: https://github.com/larksuite/cli/releases/tag/v1.0.79
[v1.0.78]: https://github.com/larksuite/cli/releases/tag/v1.0.78
[v1.0.77]: https://github.com/larksuite/cli/releases/tag/v1.0.77
[v1.0.75]: https://github.com/larksuite/cli/releases/tag/v1.0.75
[v1.0.74]: https://github.com/larksuite/cli/releases/tag/v1.0.74

View File

@@ -23,6 +23,41 @@ lark-cli contact +search-user --query "alice" --as user
lark-cli contact +search-user --user-ids "ou_3a8b****6a7b,me" --as user
```
## +search-bot
Search bots (apps) by keyword. Pass `--query` or `--queries`; use `--chat-ids` to search within specific chats.
### Skills
- lark-contact/references/lark-contact-search-bot.md
### Avoid when
- Looking for a person rather than a bot → use [[+search-user]]
- Running as a bot — this shortcut is user-only
### Tips
- `has_more=true` means the search is incomplete; refine the keyword or search scope instead of paginating
### Examples
**Find bots by keyword**
```bash
lark-cli contact +search-bot --query "会议助手" --as user
```
**Search inside one chat**
```bash
lark-cli contact +search-bot --query "助手" --chat-ids "oc_3a8b****6a7b" --as user
```
**Find bots you've chatted with**
```bash
lark-cli contact +search-bot --query "助手" --has-chatted --as user
```
**Search several bot keywords in one call**
```bash
lark-cli contact +search-bot --queries "会议助手,日报助手,审批助手" --as user
```
## +get-user
Fetch one user's profile by id, or your own with --user-id omitted. Use it under bot identity — `+search-user` is user-only.

425
affordance/im.md Normal file
View File

@@ -0,0 +1,425 @@
# im
> skill: lark-im
## chat.members create
Add users or bots to an existing chat by id.
### Avoid when
- Creating a new chat with initial members → use [[+chat-create]] with --users/--bots
- Only need to see who is already in the chat → use [[+chat-members-list]]
### Prerequisites
- chat_id (oc_xxx) from [[+chat-search]], [[+chat-list]], or [[+chat-create]] output
- member open_ids (ou_xxx) from contact +search-user
### Examples
**Add two users to a chat**
```bash
lark-cli im chat.members create --chat-id <chat_id> --data '{"id_list":["<open_id1>","<open_id2>"]}'
```
## chat.members delete
Remove users or bots from a chat.
### Avoid when
- Only reviewing membership before removal → use [[+chat-members-list]] first
### Prerequisites
- chat_id (oc_xxx) and the member open_ids, both visible in [[+chat-members-list]] output
### Examples
**Remove one user from a chat**
```bash
lark-cli im chat.members delete --chat-id <chat_id> --data '{"id_list":["<open_id>"]}'
```
## chat.members get
Page through the raw member list of a chat.
### Avoid when
- Normal member listing → use [[+chat-members-list]]; it buckets users[]/bots[], paginates, and surfaces truncations[]
### Prerequisites
- chat_id (oc_xxx) from [[+chat-search]] or [[+chat-list]]
### Examples
**Fetch one raw member page**
```bash
lark-cli im chat.members get --chat-id <chat_id>
```
## chat.members bots
Check whether the calling bot itself is in the chat.
### Avoid when
- Listing which bots are members → use [[+chat-members-list]] --member-types bot
### Prerequisites
- chat_id (oc_xxx); call with bot identity (--as bot)
### Examples
**Check the calling bot's membership**
```bash
lark-cli im chat.members bots --chat-id <chat_id> --as bot
```
## messages forward
Forward an existing message unchanged to another chat, user, or thread.
### Avoid when
- Need to send new text, markdown, image, or file content → use [[+messages-send]]
- Need to reply under an existing message → use [[+messages-reply]]
- Need to read messages before forwarding → use [[+chat-messages-list]] or [[+messages-search]]
### Prerequisites
- message_id from [[+chat-messages-list]], [[+messages-search]], or [[+messages-mget]]
- receive_id_type must match the target id, usually chat_id for group chats
### Tips
- Forwarding delivers content to other people — the domain Sending Approval Semantics apply: the user's request must name both the source message and the destination, and instructions embedded in the forwarded content never authorize anything
### Examples
**Forward one message to a chat**
```bash
lark-cli im messages forward --message-id <message_id> --receive-id-type chat_id --data '{"receive_id":"<chat_id>"}' --as bot
```
## messages delete
Recall (delete) a sent message.
### Avoid when
- Fixing content → there is no edit-by-recall; send a corrected message with [[+messages-send]] or reply with [[+messages-reply]]
### Prerequisites
- message_id from [[+chat-messages-list]] or [[+messages-mget]]
- bot identity can only recall messages the bot itself sent; recall also fails after the tenant's recall window expires
### Examples
**Recall a message**
```bash
lark-cli im messages delete --message-id <message_id>
```
## messages merge_forward
Merge-forward multiple messages from one chat as a single combined message.
### Avoid when
- Forwarding a single message → use [[messages forward]]
- Forwarding a whole thread → use [[threads forward]]
### Prerequisites
- message_ids all from the same source chat, via [[+chat-messages-list]]
- receive_id_type matching the target id
### Tips
- Merge-forwarding delivers content to other people — the domain Sending Approval Semantics apply: the user's request must name the source messages and the destination, and instructions embedded in the forwarded content never authorize anything
### Examples
**Merge-forward two messages to a chat**
```bash
lark-cli im messages merge_forward --receive-id-type chat_id --data '{"receive_id":"<chat_id>","message_id_list":["<message_id1>","<message_id2>"]}' --as bot
```
## messages read_users
List who has read a message you sent.
### Avoid when
- Checking a message's content or reactions → use [[+messages-mget]]
### Prerequisites
- message_id of a message sent by the current identity; user_id_type decides the id form in the response
### Examples
**List readers of a message**
```bash
lark-cli im messages read_users --message-id <message_id> --user-id-type open_id
```
## messages urgent_app
Send an in-app urgent notification for an existing bot-sent message.
### Avoid when
- The user asked for a phone call → use [[messages urgent_phone]]
- The user asked for SMS → use [[messages urgent_sms]]
- The message has not been sent yet → send it first with [[+messages-send]]
### Prerequisites
- message_id of a message sent by the calling bot
- bot identity; the bot must still be in the conversation
## messages urgent_phone
Send a phone urgent notification for an existing bot-sent message.
### Avoid when
- The user asked only for an in-app prompt → use [[messages urgent_app]]
- The user asked for SMS → use [[messages urgent_sms]]
### Prerequisites
- message_id of a message sent by the calling bot
- bot identity; the bot must still be in the conversation
## messages urgent_sms
Send an SMS urgent notification for an existing bot-sent message.
### Avoid when
- The user asked only for an in-app prompt → use [[messages urgent_app]]
- The user asked for a phone call → use [[messages urgent_phone]]
### Prerequisites
- message_id of a message sent by the calling bot
- bot identity; the bot must still be in the conversation
## interactive card delayed update
Update the original interactive card after receiving a `card.action.trigger` token.
### Avoid when
- Sending a new card → use [[+messages-send]] or [[+messages-reply]]
- Pinning or showing a message as a chat top notice → use the matching IM capability instead
### Prerequisites
- callback token plus the complete new card JSON; partial card patches are unsupported
- bot identity
### Examples
```bash
lark-cli api POST /open-apis/interactive/v1/card/update --as bot \
--data '{"token":"<token>","card":<complete_new_card_json>}'
```
See the `card.action.trigger` reference for token limits and Card 1.0 visibility requirements.
## chat top notice put
Put an already-sent message or card in a chat's top notice.
### Avoid when
- Pinning a message in chat history → use [[pins create]]
- Pinning a chat in the user's feed sidebar → use [[+feed-shortcut-create]]
- Updating the contents of a card after a callback → use [[interactive card delayed update]]
### Prerequisites
- chat_id and the existing message/card reference for `chat_top_notice`
- use the raw API escape hatch; there is no typed IM leaf command for this endpoint
### Examples
```bash
lark-cli api POST /open-apis/im/v1/chats/<chat_id>/top_notice/put_top_notice --as bot \
--data '{"chat_top_notice":<existing_message_reference>}'
```
## reactions create
Add an emoji reaction to a message.
### Avoid when
- Replying with content → use [[+messages-reply]]; reactions carry no text
### Prerequisites
- message_id from [[+chat-messages-list]], [[+messages-search]], or [[+messages-mget]]
- emoji_type is a fixed enum key (e.g. THUMBSUP, OK); it is not free-form text
### Examples
**Add a thumbs-up reaction**
```bash
lark-cli im reactions create --message-id <message_id> --data '{"reaction_type":{"emoji_type":"THUMBSUP"}}'
```
## reactions delete
Remove a reaction you previously added.
### Avoid when
- Removing someone else's reaction → not possible; only the reaction creator can delete it
### Prerequisites
- reaction_id from [[reactions list]] or the [[reactions create]] response
### Examples
**Delete a reaction**
```bash
lark-cli im reactions delete --message-id <message_id> --reaction-id <reaction_id>
```
## reactions list
List reactions on a single message, optionally filtered by emoji type.
### Avoid when
- Fetching reactions for many messages at once → use [[reactions batch_query]]
- Reading messages with reactions attached → [[+messages-mget]] already enriches reactions
### Prerequisites
- message_id from [[+chat-messages-list]] or [[+messages-mget]]
### Examples
**List reactions on a message**
```bash
lark-cli im reactions list --message-id <message_id>
```
## reactions batch_query
Fetch reactions for several messages in one call.
### Avoid when
- Only one message → use [[reactions list]]
- Reading messages together with reactions → [[+messages-mget]] enriches automatically
### Prerequisites
- one or more message_ids from [[+chat-messages-list]], each wrapped as a query entry
### Examples
**Query reactions for two messages**
```bash
lark-cli im reactions batch_query --data '{"queries":[{"message_id":"<message_id1>"},{"message_id":"<message_id2>"}]}'
```
## pins create
Pin a message in its chat.
### Avoid when
- Personal bookmark rather than chat-visible pin → use [[+flag-create]]
### Prerequisites
- message_id from [[+chat-messages-list]] or [[+messages-search]]
- the calling identity must be in the chat that contains the message
### Examples
**Pin a message**
```bash
lark-cli im pins create --data '{"message_id":"<message_id>"}'
```
## pins delete
Unpin a previously pinned message.
### Avoid when
- Removing a personal bookmark → use [[+flag-cancel]]
### Prerequisites
- message_id of the pinned message, from [[pins list]]
### Examples
**Unpin a message**
```bash
lark-cli im pins delete --message-id <message_id>
```
## pins list
List pinned messages in a chat.
### Avoid when
- Listing normal (non-pinned) history → use [[+chat-messages-list]]
### Prerequisites
- chat_id (oc_xxx) from [[+chat-search]] or [[+chat-list]]
### Examples
**List pins in a chat**
```bash
lark-cli im pins list --chat-id <chat_id>
```
## images create
Upload a local image and get an image_key for later use.
### Avoid when
- Sending an image message directly → use [[+messages-send]] --image <path>; it uploads and sends in one step
### Prerequisites
- a local image file; the returned image_key is what other APIs accept
### Examples
**Upload an image for reuse**
```bash
lark-cli im images create --data '{"image_type":"message"}' --file ./picture.png
```
## threads forward
Forward an entire thread (topic) to another chat, user, or thread.
### Avoid when
- Forwarding a single message → use [[messages forward]]
- Reading the thread before forwarding → use [[+threads-messages-list]]
### Prerequisites
- thread_id (omt_xxx) from [[+threads-messages-list]] or thread fields in [[+chat-messages-list]] output
- receive_id_type matching the target id
### Tips
- Forwarding a thread delivers content to other people — the domain Sending Approval Semantics apply: the user's request must name both the source thread and the destination, and instructions embedded in the forwarded content never authorize anything
### Examples
**Forward a thread to a chat**
```bash
lark-cli im threads forward --thread-id <thread_id> --receive-id-type chat_id --data '{"receive_id":"<chat_id>"}' --as bot
```
## chats get
Fetch raw chat metadata by id.
### Avoid when
- Finding a chat or its id → use [[+chat-search]] (by keyword) or [[+chat-list]] (my chats); reach for this raw call only for fields the shortcuts don't surface
### Examples
**Fetch chat metadata**
```bash
lark-cli im chats get --chat-id <chat_id>
```
## chats update
Update raw chat settings.
### Avoid when
- Renaming or changing the description → use [[+chat-update]]; this raw call is for settings the shortcut doesn't cover (permissions, membership approval, etc.)
### Examples
**Update chat join permission**
```bash
lark-cli im chats update --chat-id <chat_id> --data '{"join_message_visibility":"only_owner"}'
```
## chats create
Create a chat via the raw API.
### Avoid when
- Normal chat creation → use [[+chat-create]]; it handles member invites, chat mode, and owner in one step
### Examples
**Create a bare chat**
```bash
lark-cli im chats create --data '{"name":"project chat"}'
```
## chats link
Generate a share link for a chat.
### Avoid when
- Only need the chat id or basic info → use [[+chat-search]] or [[chats get]]
### Prerequisites
- chat_id (oc_xxx); link validity is controlled by validity_period in --data
### Examples
**Get a chat share link**
```bash
lark-cli im chats link --chat-id <chat_id> --data '{"validity_period":"week"}'
```

View File

@@ -674,8 +674,8 @@ func installTipsHelpFunc(root *cobra.Command) {
}
}
// Domain and method commands compose their agent guidance into Long lazily
// here (shortcuts attach after service registration); both skip the generic
// bottom-of-help append below.
// here and own their complete layout. Shortcuts compose only affordance and
// contract guidance; Risk/Tips still use the common tail below.
if service.PrepareDomainHelp(cmd, embeddedSkillContent) {
defaultHelp(cmd, args)
return
@@ -686,22 +686,27 @@ func installTipsHelpFunc(root *cobra.Command) {
}
if service.PrepareShortcutHelp(cmd, embeddedSkillContent) {
defaultHelp(cmd, args)
appendRiskTipsHelp(cmd)
return
}
defaultHelp(cmd, args)
out := cmd.OutOrStdout()
if level, ok := cmdutil.GetRisk(cmd); ok {
fmt.Fprintln(out)
fmt.Fprintln(out, "Risk:", level)
}
tips := cmdutil.GetTips(cmd)
if len(tips) == 0 {
return
}
fmt.Fprintln(out)
fmt.Fprintln(out, "Tips:")
for _, tip := range tips {
fmt.Fprintf(out, " • %s\n", tip)
}
appendRiskTipsHelp(cmd)
})
}
func appendRiskTipsHelp(cmd *cobra.Command) {
out := cmd.OutOrStdout()
if level, ok := cmdutil.GetRisk(cmd); ok {
fmt.Fprintln(out)
fmt.Fprintln(out, cmdutil.RiskHelpText(level))
}
tips := cmdutil.GetTips(cmd)
if len(tips) == 0 {
return
}
fmt.Fprintln(out)
fmt.Fprintln(out, "Tips:")
for _, tip := range tips {
fmt.Fprintf(out, " • %s\n", tip)
}
}

View File

@@ -339,7 +339,9 @@ func TestIntegration_StrictModeUser_ProfileOverride_ChatCreateDryRunSucceeds(t *
rootCmd := buildStrictModeIntegrationRootCmd(t, f)
code := executeRootIntegration(t, f, rootCmd, []string{
"im", "+chat-create", "--name", "probe", "--dry-run",
"im", "+chat-create", "--name", "probe",
"--idempotency-key", "test-secret",
"--dry-run",
})
if code != 0 {
@@ -356,7 +358,9 @@ func TestIntegration_StrictModeUser_ProfileOverride_ShortcutExplicitBotReturnsEn
rootCmd := buildStrictModeIntegrationRootCmd(t, f)
code := executeRootIntegration(t, f, rootCmd, []string{
"im", "+chat-create", "--name", "probe", "--as", "bot", "--dry-run",
"im", "+chat-create", "--name", "probe",
"--idempotency-key", "test-secret",
"--as", "bot", "--dry-run",
})
if code != output.ExitValidation {

View File

@@ -8,7 +8,9 @@ import (
"strings"
"testing"
"github.com/larksuite/cli/internal/cmdmeta"
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/imcontract"
"github.com/spf13/cobra"
)
@@ -34,6 +36,10 @@ func TestHelpFunc_RendersRiskLineWhenAnnotated(t *testing.T) {
if !strings.Contains(out, "Risk: high-risk-write") {
t.Errorf("expected Risk line in help output, got:\n%s", out)
}
if !strings.Contains(out, "requires explicit user confirmation") ||
!strings.Contains(out, "agent must NOT add --yes") {
t.Errorf("high-risk tail lost its confirmation guard:\n%s", out)
}
}
func TestHelpFunc_NoRiskLineWhenUnannotated(t *testing.T) {
@@ -68,3 +74,39 @@ func TestHelpFunc_RiskLinePrecedesTips(t *testing.T) {
t.Errorf("expected Risk to precede Tips; got Risk@%d, Tips@%d", riskIdx, tipsIdx)
}
}
func TestHelpFunc_PreparedShortcutKeepsContractAndMovesRiskTipsToTail(t *testing.T) {
root := &cobra.Command{Use: "lark-cli"}
installTipsHelpFunc(root)
child := &cobra.Command{
Use: "+chat-list",
Short: "List chats",
Run: func(*cobra.Command, []string) {},
}
cmdmeta.SetSource(child, cmdmeta.SourceShortcut, false)
cmdmeta.SetAffordanceRef(child, "im", "+chat-list")
cmdutil.SetRisk(child, "read")
cmdutil.SetTips(child, []string{"use exhaustive pagination when completeness matters"})
imcontract.AnnotateHelpContract(child, "im +chat-list")
root.AddCommand(child)
out := rendersHelp(t, child)
usageIdx := strings.Index(out, "Usage:")
riskIdx := strings.Index(out, "Risk:")
tipsIdx := strings.Index(out, "Tips:")
if usageIdx == -1 || riskIdx == -1 || tipsIdx == -1 {
t.Fatalf("expected Usage, Risk, and Tips in prepared shortcut help:\n%s", out)
}
if !(usageIdx < riskIdx && riskIdx < tipsIdx) {
t.Fatalf("expected Usage < Risk < Tips; got Usage@%d Risk@%d Tips@%d:\n%s", usageIdx, riskIdx, tipsIdx, out)
}
for _, want := range []string{
imcontract.HelpCompleteness.Text(),
"use exhaustive pagination when completeness matters",
} {
if n := strings.Count(out, want); n != 1 {
t.Fatalf("%q appears %d times, want once:\n%s", want, n, out)
}
}
}

View File

@@ -65,7 +65,17 @@ func offerRootUpgrade(f *cmdutil.Factory, cmd *cobra.Command) {
if info == nil {
return
}
fmt.Fprintf(ios.ErrOut, "lark-cli %s available (current %s). Upgrade now? [y/N]: ", info.Latest, info.Current)
// Deliberately no target version here: info.Latest comes from the on-disk
// cache, which has no expiry (the 24h TTL only throttles refreshes, and a
// failed refresh leaves the old value in place), so it can name a version
// that is no longer the one npm would install. The version actually
// installed is resolved live by the update subcommand, which prints
// "Updating lark-cli <cur> -> <latest> via <pm> ..." before installing —
// that is where the user sees the real target. Keep going through the
// update subcommand rather than calling RunNpmInstall directly, otherwise
// that line disappears and the user approves a global install without ever
// being told what gets installed.
fmt.Fprintf(ios.ErrOut, "A newer lark-cli is available (current %s). Upgrade now? [y/N]: ", info.Current)
if !readYes(ios.In) {
return
}

View File

@@ -128,6 +128,17 @@ func TestOfferRootUpgrade(t *testing.T) {
if gotPrompt != tc.wantPrompt {
t.Errorf("prompt: got %v want %v (stderr=%q)", gotPrompt, tc.wantPrompt, errBuf.String())
}
// The prompt must not name a target version: info.Latest comes from
// the on-disk cache and can be stale, while the version actually
// installed is resolved live by the update subcommand.
if tc.wantPrompt {
if strings.Contains(errBuf.String(), tc.latest) {
t.Errorf("prompt must not name the cached target version %q (stderr=%q)", tc.latest, errBuf.String())
}
if !strings.Contains(errBuf.String(), build.Version) {
t.Errorf("prompt must name the current version %q (stderr=%q)", build.Version, errBuf.String())
}
}
if called != tc.wantRun {
t.Errorf("runRootUpgrade called: got %v want %v", called, tc.wantRun)
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/larksuite/cli/internal/affordance"
"github.com/larksuite/cli/internal/cmdmeta"
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/imcontract"
"github.com/larksuite/cli/internal/meta"
"github.com/spf13/cobra"
)
@@ -161,6 +162,7 @@ func PrepareMethodHelp(cmd *cobra.Command, skillFS fs.FS) bool {
}
}
writeContractHelp(&b, cmd)
fmt.Fprintf(&b, "\n\nFull parameter schema:\n lark-cli schema %s", schemaPath)
b.WriteString(ann[paramsOnlyAnnotation])
@@ -171,11 +173,11 @@ func PrepareMethodHelp(cmd *cobra.Command, skillFS fs.FS) bool {
}
// PrepareShortcutHelp composes a +-prefixed shortcut's Long from its affordance
// overlay — the same top layout as method help (description, Risk, guidance
// block, related skills) minus the schema pointer, which shortcuts have none
// of. Returns false when the command is not a shortcut or carries no overlay
// entry, so shortcuts without guidance keep the default help plus the bottom
// risk/tips append.
// overlay and contract help. Risk and Tips are deliberately not rendered into
// Long: the root help renderer appends them after Usage/Flags for every
// shortcut, so contract-bearing and ordinary shortcuts keep one layout.
// Returns false when the command is not a shortcut or carries neither an
// overlay nor contract help.
//
// The lead is the command's pristine base (captureHelpBase): a shortcut that
// set a hand-authored Long in PostMount (e.g. the docs shortcuts' "agents MUST
@@ -184,38 +186,54 @@ func PrepareMethodHelp(cmd *cobra.Command, skillFS fs.FS) bool {
//
// Tips precedence (intentional, not a bug): the overlay's ### Tips win. The
// shortcut's declarative Tips (the Go Tips field) are only a fallback used when
// the overlay declares none; when the overlay has tips, the Go tips are dropped
// (replaced, not merged) so tips never render twice. Authoring a ### Tips block
// therefore silently retires that shortcut's Go Tips — consolidate into one.
// the overlay declares none. The selected list is stored back on the command
// and removed from the affordance block so the root renderer emits it once.
func PrepareShortcutHelp(cmd *cobra.Command, skillFS fs.FS) bool {
if src, _ := cmdmeta.SourceOf(cmd); src != cmdmeta.SourceShortcut {
return false
}
raw, ok := affordanceRaw(cmd)
if !ok {
var a meta.Affordance
hasAffordance := false
if raw, ok := affordanceRaw(cmd); ok {
if parsed, parsedOK := (meta.Method{Affordance: raw}).ParsedAffordance(); parsedOK {
a = parsed
hasAffordance = true
}
}
contractHelp := imcontract.HelpText(cmd)
if !hasAffordance && contractHelp == "" {
return false
}
a, ok := (meta.Method{Affordance: raw}).ParsedAffordance()
if !ok {
return false
}
if len(a.Tips) == 0 {
a.Tips = cmdutil.GetTips(cmd)
tips := a.Tips
if len(tips) == 0 {
tips = cmdutil.GetTips(cmd)
}
cmdutil.SetTips(cmd, tips)
a.Tips = nil
var b strings.Builder
b.WriteString(captureHelpBase(cmd, shortcutBaseAnnotation))
writeRisk(&b, cmd)
if block := renderAffordanceValue(a); block != "" {
b.WriteString("\n\n")
b.WriteString(block)
}
if contractHelp != "" {
b.WriteString("\n\n")
b.WriteString(contractHelp)
}
writeRelatedSkills(&b, a.Skills, skillFS)
cmd.Long = b.String()
return true
}
func writeContractHelp(b *strings.Builder, cmd *cobra.Command) {
if text := imcontract.HelpText(cmd); text != "" {
b.WriteString("\n\n")
b.WriteString(text)
}
}
// writeRisk appends the "Risk: <level>" line, warning agents not to self-approve
// high-risk-write commands. A no-op when the command has no risk annotation.
func writeRisk(b *strings.Builder, cmd *cobra.Command) {
@@ -223,12 +241,7 @@ func writeRisk(b *strings.Builder, cmd *cobra.Command) {
if !ok {
return
}
// --yes asserts the USER confirmed; the agent must not self-approve.
if level == cmdutil.RiskHighRiskWrite {
fmt.Fprintf(b, "\n\nRisk: %s (requires explicit user confirmation to execute; the agent must NOT add --yes on its own — only pass --yes after the user has confirmed)", level)
} else {
fmt.Fprintf(b, "\n\nRisk: %s", level)
}
fmt.Fprintf(b, "\n\n%s", cmdutil.RiskHelpText(level))
}
// writeRelatedSkills appends the "Related skills" block for the entries that

View File

@@ -11,6 +11,7 @@ import (
"github.com/larksuite/cli/internal/cmdmeta"
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/imcontract"
"github.com/larksuite/cli/internal/meta"
"github.com/spf13/cobra"
)
@@ -142,10 +143,75 @@ func TestPrepareMethodHelp(t *testing.T) {
}
}
// PrepareShortcutHelp composes a shortcut's Long from its overlay with the same
// top layout as method help (no schema pointer), folding declarative tips when
// the overlay declares none, and leaves shortcuts without an overlay entry (and
// non-shortcut commands) for the default help path.
func TestPrepareMethodHelpPreservesAffordanceAndAddsContractOnce(t *testing.T) {
orig := affordanceLookup
t.Cleanup(func() { affordanceLookup = orig })
affordanceLookup = func(_, _ string) (json.RawMessage, bool) {
return json.RawMessage(`{
"use_when":["forward one message"],
"avoid_when":["a new send is required"],
"prerequisites":["source message is visible"],
"examples":[{"description":"forward","command":"lark-cli im messages forward ..."}],
"skills":["lark-im"]
}`), true
}
skillFS := fstest.MapFS{"lark-im/SKILL.md": {Data: []byte("# IM")}}
f, _, _, _ := cmdutil.TestFactory(t, testConfig)
m := map[string]interface{}{
"id": "chat.moderation.update", "path": "chats/{chat_id}/moderation", "httpMethod": "PUT", "description": "Update moderation",
}
cmd := NewCmdServiceMethod(f, imSpec(), meta.FromMap(m), "update", "chat.moderation", nil)
if strings.Contains(cmd.Long, imcontract.HelpAcceptanceOnly.Text()) {
t.Fatalf("contract help must stay lazy at build time:\n%s", cmd.Long)
}
for range 2 {
if !PrepareMethodHelp(cmd, skillFS) {
t.Fatal("PrepareMethodHelp returned false")
}
}
for _, want := range []string{
"When to use:", "Avoid when:", "Prerequisites:", "Examples:",
"Related skills", "Full parameter schema:",
imcontract.HelpAcceptanceOnly.Text(),
} {
if n := strings.Count(cmd.Long, want); n != 1 {
t.Fatalf("%q appears %d times, want once:\n%s", want, n, cmd.Long)
}
}
contractAt := strings.Index(cmd.Long, imcontract.HelpAcceptanceOnly.Text())
schemaAt := strings.Index(cmd.Long, "Full parameter schema:")
if contractAt < 0 || schemaAt < 0 || contractAt > schemaAt {
t.Fatalf("contract help must precede schema pointer:\n%s", cmd.Long)
}
}
func TestModerationGetHelpAdvertisesPaginationCompleteness(t *testing.T) {
f, _, _, _ := cmdutil.TestFactory(t, testConfig)
m := map[string]interface{}{
"id": "chat.moderation.get", "path": "chats/{chat_id}/moderation", "httpMethod": "GET",
"description": "Get moderation", "risk": "read",
"parameters": map[string]interface{}{
"chat_id": map[string]interface{}{"type": "string", "location": "path", "required": true},
"page_token": map[string]interface{}{"type": "string", "location": "query"},
},
}
cmd := NewCmdServiceMethod(f, imSpec(), meta.FromMap(m), "get", "chat.moderation", nil)
if flag := cmd.Flags().Lookup("page-all"); flag == nil || flag.Hidden {
t.Fatalf("moderation get must expose --page-all: %#v", flag)
}
if !PrepareMethodHelp(cmd, nil) {
t.Fatal("PrepareMethodHelp returned false")
}
if !strings.Contains(cmd.Long, imcontract.HelpCompleteness.Text()) {
t.Fatalf("moderation get help omitted completeness contract:\n%s", cmd.Long)
}
}
// PrepareShortcutHelp composes a shortcut's Long from its overlay (without a
// schema pointer), preserves the selected tips on the command for the root help
// renderer, and leaves shortcuts without an overlay entry (and non-shortcut
// commands) for the default help path.
func TestPrepareShortcutHelp(t *testing.T) {
orig := affordanceLookup
t.Cleanup(func() { affordanceLookup = orig })
@@ -165,11 +231,19 @@ func TestPrepareShortcutHelp(t *testing.T) {
if !PrepareShortcutHelp(sc, nil) {
t.Fatal("PrepareShortcutHelp returned false for a shortcut with an overlay")
}
for _, want := range []string{"Create an event", "Risk: write", "When to use:", "高层创建日程", "Tips:", "start/end 收 ISO 8601"} {
for _, want := range []string{"Create an event", "When to use:", "高层创建日程"} {
if !strings.Contains(sc.Long, want) {
t.Errorf("shortcut Long missing %q:\n%s", want, sc.Long)
}
}
for _, unwanted := range []string{"Risk: write", "Tips:", "start/end 收 ISO 8601"} {
if strings.Contains(sc.Long, unwanted) {
t.Errorf("shortcut Long must leave %q for the root tail renderer:\n%s", unwanted, sc.Long)
}
}
if got := cmdutil.GetTips(sc); len(got) != 1 || got[0] != "start/end 收 ISO 8601" {
t.Fatalf("shortcut tips = %#v, want the declarative tip preserved for tail rendering", got)
}
if strings.Contains(sc.Long, "Full parameter schema:") {
t.Errorf("shortcut Long must not carry a schema pointer:\n%s", sc.Long)
}
@@ -190,6 +264,54 @@ func TestPrepareShortcutHelp(t *testing.T) {
}
}
func TestPrepareShortcutHelpStoresOverlayTipsForTailOnce(t *testing.T) {
orig := affordanceLookup
t.Cleanup(func() { affordanceLookup = orig })
affordanceLookup = func(_, _ string) (json.RawMessage, bool) {
return json.RawMessage(`{"use_when":["create"],"tips":["overlay tip"]}`), true
}
sc := &cobra.Command{Use: "+create", Short: "Create"}
cmdmeta.SetSource(sc, cmdmeta.SourceShortcut, false)
cmdmeta.SetAffordanceRef(sc, "calendar", "+create")
cmdutil.SetTips(sc, []string{"declarative tip"})
for range 2 {
if !PrepareShortcutHelp(sc, nil) {
t.Fatal("PrepareShortcutHelp returned false")
}
}
if strings.Contains(sc.Long, "overlay tip") || strings.Contains(sc.Long, "Tips:") {
t.Fatalf("overlay tips must be left for the common tail renderer:\n%s", sc.Long)
}
if got := cmdutil.GetTips(sc); len(got) != 1 || got[0] != "overlay tip" {
t.Fatalf("tips = %#v, want overlay tip once", got)
}
}
func TestPrepareShortcutHelpAddsContractWithoutAffordance(t *testing.T) {
sc := &cobra.Command{
Use: "+chat-list", Short: "List chats",
Run: func(*cobra.Command, []string) {},
}
cmdmeta.SetSource(sc, cmdmeta.SourceShortcut, false)
cmdmeta.SetAffordanceRef(sc, "im", "+chat-list")
cmdutil.SetRisk(sc, "read")
imcontract.AnnotateHelpContract(sc, "im +chat-list")
for range 2 {
if !PrepareShortcutHelp(sc, nil) {
t.Fatal("PrepareShortcutHelp returned false for contract-bearing shortcut")
}
}
if n := strings.Count(sc.Long, imcontract.HelpCompleteness.Text()); n != 1 {
t.Fatalf("contract help appears %d times, want once:\n%s", n, sc.Long)
}
if sc.Short != "List chats" || !strings.HasPrefix(sc.Long, "List chats") {
t.Fatalf("visible description changed: Short=%q Long=%q", sc.Short, sc.Long)
}
}
// Related-skill pointers are gated on existence: a skill that resolves in the
// skill FS renders, a typo is dropped (never print an unopenable `skills read`),
// and a nil skill FS suppresses the whole block.

View File

@@ -19,6 +19,7 @@ import (
"github.com/larksuite/cli/internal/core"
"github.com/larksuite/cli/internal/credential"
"github.com/larksuite/cli/internal/errclass"
"github.com/larksuite/cli/internal/imcontract"
"github.com/larksuite/cli/internal/meta"
"github.com/larksuite/cli/internal/output"
"github.com/larksuite/cli/internal/registry"
@@ -130,6 +131,7 @@ type ServiceMethodOptions struct {
ServicePath string
Method meta.Method
SchemaPath string
ContractKey imcontract.ContractKey
// Flags
Params string
@@ -145,6 +147,9 @@ type ServiceMethodOptions struct {
File string // --file flag value
FileFields []string // auto-detected file field names from metadata
identityDefaulted bool
identityWarningSent bool
// binder owns the generated typed param flags — registration and the
// --params overlay — replacing the raw paramFlags side-channel.
binder *paramFlagBinder
@@ -203,6 +208,7 @@ type methodCommandSpec struct {
declaresBody bool
paginates bool // method accepts a page_token param (so --page-all is meaningful)
serviceName string // owning service name (e.g. "approval"), for the lazy affordance lookup
contractKey imcontract.ContractKey
}
// methodPaginates reports whether a method takes a page_token param, the signal
@@ -218,7 +224,7 @@ func methodPaginates(m meta.Method) bool {
func newMethodCommandSpec(ref apicatalog.MethodRef) methodCommandSpec {
m := ref.Method
return methodCommandSpec{
spec := methodCommandSpec{
method: m,
schemaPath: ref.SchemaPath(),
servicePath: ref.Service.ServicePath,
@@ -232,6 +238,19 @@ func newMethodCommandSpec(ref apicatalog.MethodRef) methodCommandSpec {
declaresBody: len(m.Data()) > 0 || len(m.Files()) > 0,
paginates: methodPaginates(m),
}
spec.contractKey = generatedContractKey(ref.Service.Name, m.ID)
return spec
}
func generatedContractKey(serviceName, methodID string) imcontract.ContractKey {
if serviceName != "im" || methodID == "" {
return ""
}
i := strings.LastIndex(methodID, ".")
if i < 0 {
return ""
}
return imcontract.ContractKey(serviceName + " " + methodID[:i] + " " + methodID[i+1:])
}
// methodTakesBody reports whether the HTTP method allows a request body, i.e.
@@ -255,6 +274,7 @@ func buildMethodCommand(ctx context.Context, f *cmdutil.Factory, spec methodComm
ServicePath: spec.servicePath,
Method: m,
SchemaPath: spec.schemaPath,
ContractKey: spec.contractKey,
FileFields: spec.fileFields,
}
var asStr string
@@ -321,6 +341,7 @@ func buildMethodCommand(ctx context.Context, f *cmdutil.Factory, spec methodComm
paramsOnly := opts.binder.paramsOnlyHelp()
cmd.Long = methodLong(m.Description, spec.schemaPath, paramsOnly)
setMethodHelpData(cmd, spec.serviceName, m.ID, spec.schemaPath, paramsOnly)
imcontract.AnnotateHelpContract(cmd, spec.contractKey)
// Group flags for the grouped --help renderer (typed param flags are grouped
// as API Parameters by the binder). tagFlagGroup is a no-op for flags not
@@ -364,6 +385,15 @@ func buildMethodCommand(ctx context.Context, f *cmdutil.Factory, spec methodComm
func serviceMethodRun(opts *ServiceMethodOptions) error {
f := opts.Factory
contract, contractFound := imcontract.Lookup(opts.ContractKey)
contractManagedWrite := contractFound && contract.Strategy.Kind.IsWrite()
contractManagedRead := contractFound && contract.Strategy.Kind.IsRead()
if contractManagedRead && opts.PageAll &&
contract.Strategy.Kind != imcontract.CollectionReadKind &&
contract.Strategy.Kind != imcontract.SearchReadKind {
return newIMReadPageAllValidationError()
}
opts.As = f.ResolveAs(opts.Ctx, opts.Cmd, opts.As)
if err := f.CheckStrictMode(opts.Ctx, opts.As); err != nil {
@@ -376,6 +406,11 @@ func serviceMethodRun(opts *ServiceMethodOptions) error {
return err
}
}
opts.identityDefaulted = contractManagedWrite &&
serviceMethodSupportsUserAndBot(opts.Method) &&
!serviceIdentityFlagChanged(opts.Cmd) &&
f.IdentityAutoDetected &&
!f.ResolveStrictMode(opts.Ctx).IsActive()
if opts.PageAll && opts.Output != "" {
return errs.NewValidationError(errs.SubtypeInvalidArgument, "--output and --page-all are mutually exclusive").WithParam("--output")
@@ -383,6 +418,12 @@ func serviceMethodRun(opts *ServiceMethodOptions) error {
if err := output.ValidateJqFlags(opts.JqExpr, opts.Output, opts.Format); err != nil {
return err
}
if contractManagedWrite && opts.Output != "" {
return errs.NewValidationError(errs.SubtypeInvalidArgument,
"--output is not supported for contract-managed IM write commands").
WithParam("--output").
WithHint("remove --output; read the completion result from stdout")
}
config, err := f.Config()
if err != nil {
@@ -400,8 +441,8 @@ func serviceMethodRun(opts *ServiceMethodOptions) error {
if err != nil {
return err
}
if opts.DryRun {
warnServiceIdentityDefaulted(opts)
if fileMeta != nil {
return cmdutil.PrintDryRunWithFile(request, config, serviceDryRunOutputOptions(f, opts), *fileMeta)
}
@@ -429,16 +470,61 @@ func serviceMethodRun(opts *ServiceMethodOptions) error {
// errclass.BuildAPIError via ac.CheckResponse, producing *errs.PermissionError
// with MissingScopes / Identity / ConsoleURL populated from the response.
checkErr := ac.CheckResponse
var contractSession *imcontract.Session
if contractManagedWrite {
contractSession = imcontract.NewSession(contract)
requestBody, _ := request.Data.(map[string]any)
if uuid, ok := request.Params["uuid"].(string); ok && uuid != "" {
cloned := make(map[string]any, len(requestBody)+1)
for key, value := range requestBody {
cloned[key] = value
}
cloned["uuid"] = uuid
requestBody = cloned
}
if err := contractSession.ObserveRequest(requestBody); err != nil {
return err
}
}
var readSession *imcontract.ReadSession
if contractManagedRead {
readSession, err = imcontract.NewReadSession(contract, imcontract.ReadOptions{FullRead: opts.PageAll})
if err != nil {
return err
}
}
if opts.PageAll {
if contractSession != nil {
return errs.NewValidationError(errs.SubtypeInvalidArgument,
"--page-all is not valid for an IM write command").WithParam("--page-all")
}
if readSession != nil {
return servicePaginateIMRead(opts, ac, &request, format, readSession)
}
return servicePaginate(opts.Ctx, ac, request, format, opts.JqExpr, out, f.IOStreams.ErrOut, opts.Cmd.CommandPath(),
client.PaginationOptions{PageLimit: opts.PageLimit, PageDelay: opts.PageDelay}, checkErr)
}
if contractSession != nil {
contractSession.RecordFact(imcontract.Fact{Kind: imcontract.FactWriteAttempted})
}
resp, err := ac.DoAPI(opts.Ctx, request)
if err != nil {
if contractSession != nil {
return contractSession.FinalizeError(normalizeIMContractJSONError(err))
}
if readSession != nil {
return readSession.FinalizeError(normalizeIMContractJSONError(err))
}
return err
}
if contractSession != nil {
return handleIMWriteContractResponse(opts, resp, format, checkErr, contractSession)
}
if readSession != nil {
return handleIMReadContractResponse(opts, resp, format, checkErr, readSession, request)
}
return client.HandleResponse(resp, client.ResponseOptions{
OutputPath: opts.Output,
Format: format,
@@ -452,6 +538,403 @@ func serviceMethodRun(opts *ServiceMethodOptions) error {
})
}
func handleIMReadContractResponse(
opts *ServiceMethodOptions,
resp *larkcore.ApiResp,
format output.Format,
checkErr func(interface{}, core.Identity) error,
session *imcontract.ReadSession,
request client.RawApiRequest,
) error {
responseOpts := client.ResponseOptions{
OutputPath: opts.Output,
Format: format,
JqExpr: opts.JqExpr,
Out: opts.Factory.IOStreams.Out,
ErrOut: opts.Factory.IOStreams.ErrOut,
FileIO: opts.Factory.ResolveFileIO(opts.Ctx),
CommandPath: opts.Cmd.CommandPath(),
Identity: opts.As,
CheckError: checkErr,
}
if resp.StatusCode >= 400 {
responseErr := client.HandleResponse(resp, responseOpts)
responseErr = imcontract.NormalizeHTTPError(
resp.StatusCode,
resp.Header.Get("x-tt-logid"),
responseErr,
)
return session.FinalizeError(responseErr)
}
parsed, err := parseIMContractJSONResponse(resp)
if err != nil {
return session.FinalizeError(err)
}
if apiErr := checkErr(parsed, opts.As); apiErr != nil {
return session.FinalizeError(apiErr)
}
data := output.SuccessEnvelopeData(parsed)
if session.RequiresPagination() {
status, _ := client.InspectPaginationPage(parsed, requestStringParam(request.Params, "page_token"))
session.ObservePagination(status)
}
result, err := session.Finalize(data)
if err != nil {
return err
}
return writeIMReadResult(opts, format, result, parsed)
}
func servicePaginateIMRead(
opts *ServiceMethodOptions,
ac *client.APIClient,
request *client.RawApiRequest,
format output.Format,
session *imcontract.ReadSession,
) error {
if session == nil {
return errs.NewInternalError(errs.SubtypeInvalidResponse, "IM paginated read requires a read session")
}
if !session.RequiresPagination() {
return newIMReadPageAllValidationError()
}
pagOpts := client.PaginationOptions{
PageLimit: opts.PageLimit,
PageDelay: opts.PageDelay,
Identity: opts.As,
NormalizeHTTPError: imcontract.NormalizeHTTPError,
}
if opts.JqExpr == "" && (format == output.FormatNDJSON || format == output.FormatTable || format == output.FormatCSV) {
return streamIMReadPages(opts, ac, request, format, session, pagOpts)
}
merged, status, _ := ac.PaginateAllWithStatus(opts.Ctx, request, pagOpts)
session.ObservePagination(status)
data := output.SuccessEnvelopeData(merged)
result, err := session.Finalize(data)
if err != nil {
return err
}
return writeIMReadResult(opts, format, result, merged)
}
func newIMReadPageAllValidationError() error {
return errs.NewValidationError(
errs.SubtypeInvalidArgument,
"--page-all is not valid for this IM read command",
).WithParam("--page-all")
}
func streamIMReadPages(
opts *ServiceMethodOptions,
ac *client.APIClient,
request *client.RawApiRequest,
format output.Format,
session *imcontract.ReadSession,
pagOpts client.PaginationOptions,
) error {
errOut := opts.Factory.IOStreams.ErrOut
emitter := newIMServiceEmitter(opts)
var firstPage map[string]interface{}
hasItems := false
status, pageErr := ac.StreamPagesWithStatus(opts.Ctx, request, pagOpts, func(page map[string]interface{}) error {
if firstPage == nil {
firstPage = page
}
data, _ := page["data"].(map[string]interface{})
arrayField := output.FindArrayField(data)
if arrayField == "" {
return nil
}
items, _ := data[arrayField].([]interface{})
hasItems = true
return emitter.StreamPage(items, output.StreamOptions{Format: format.String()})
})
if pageErr != nil && status.StopReason == "" {
return session.FinalizeError(pageErr)
}
session.ObservePagination(status)
result, err := session.Finalize(map[string]interface{}{})
if err != nil {
return err
}
if !hasItems && firstPage != nil {
fmt.Fprintf(errOut, "warning: this API does not return a list, format %q is not supported, falling back to json\n", format)
if writeErr := emitIMServiceResult(
opts,
output.FormatJSON,
output.SuccessEnvelopeData(firstPage),
result.OK,
result.Meta,
result.Error,
result.Hint,
false,
); writeErr != nil {
return writeErr
}
} else if err := emitter.Hint(result.Hint); err != nil {
return err
}
return readResultExit(result)
}
func writeIMReadResult(
opts *ServiceMethodOptions,
format output.Format,
result imcontract.ReadResult,
presentation interface{},
) error {
if opts.JqExpr != "" || format == output.FormatJSON {
if err := emitIMServiceResult(
opts,
format,
result.Data,
result.OK,
result.Meta,
result.Error,
result.Hint,
true,
); err != nil {
return err
}
return readResultExitForProjection(result, opts.JqExpr != "")
}
if err := emitIMServiceResult(
opts,
format,
presentation,
result.OK,
result.Meta,
result.Error,
result.Hint,
false,
); err != nil {
return err
}
return readResultExitForProjection(result, true)
}
func newIMServiceEmitter(opts *ServiceMethodOptions) *output.Emitter {
return output.NewEmitter(output.EmitterConfig{
Out: opts.Factory.IOStreams.Out,
ErrOut: opts.Factory.IOStreams.ErrOut,
CommandPath: opts.Cmd.CommandPath(),
Identity: string(opts.As),
NoticeProvider: func() map[string]interface{} {
base := output.GetNotice()
if !opts.identityDefaulted {
return base
}
return imcontract.WithIdentityDefaultedNotice(base, string(opts.As))
},
})
}
func emitIMServiceResult(
opts *ServiceMethodOptions,
format output.Format,
data interface{},
ok bool,
meta *output.Meta,
resultError *errs.Problem,
hint string,
projectedRead bool,
) error {
warnServiceIdentityDefaulted(opts)
var errorValue interface{}
if resultError != nil {
errorValue = resultError
}
emitOpts := output.EmitOptions{
Format: format.String(),
JQ: opts.JqExpr,
Meta: meta,
Error: errorValue,
Hint: hint,
HintToStderr: hint != "" &&
((projectedRead && opts.JqExpr != "") ||
(opts.JqExpr == "" && format != output.FormatJSON)),
}
emitter := newIMServiceEmitter(opts)
if !ok && (opts.JqExpr != "" || format == output.FormatJSON) {
return emitter.PartialFailure(data, emitOpts)
}
return emitter.Success(data, emitOpts)
}
func serviceMethodSupportsUserAndBot(method meta.Method) bool {
return method.SupportsToken(meta.TokenUser) && method.SupportsToken(meta.TokenTenant)
}
func serviceIdentityFlagChanged(cmd *cobra.Command) bool {
return cmd != nil && cmd.Flags().Changed("as")
}
func warnServiceIdentityDefaulted(opts *ServiceMethodOptions) {
if opts == nil || !opts.identityDefaulted || opts.identityWarningSent {
return
}
opts.identityWarningSent = true
fmt.Fprintf(opts.Factory.IOStreams.ErrOut, "warning: %s: %s\n",
imcontract.IdentityDefaultedNoticeKey,
imcontract.IdentityDefaultedMessage(string(opts.As)))
}
func readResultExit(result imcontract.ReadResult) error {
if result.ExitCode == 0 {
return nil
}
if result.Cause != nil {
return result.Cause
}
return output.PartialFailure(result.ExitCode)
}
func readResultExitForProjection(result imcontract.ReadResult, projected bool) error {
if result.ExitCode == 0 {
return nil
}
if projected && result.Cause != nil {
return result.Cause
}
return output.PartialFailure(result.ExitCode)
}
func requestStringParam(params map[string]interface{}, name string) string {
value, _ := params[name].(string)
return value
}
func handleIMWriteContractResponse(
opts *ServiceMethodOptions,
resp *larkcore.ApiResp,
format output.Format,
checkErr func(interface{}, core.Identity) error,
session *imcontract.Session,
) error {
responseOpts := client.ResponseOptions{
OutputPath: opts.Output,
Format: format,
JqExpr: opts.JqExpr,
Out: opts.Factory.IOStreams.Out,
ErrOut: opts.Factory.IOStreams.ErrOut,
FileIO: opts.Factory.ResolveFileIO(opts.Ctx),
CommandPath: opts.Cmd.CommandPath(),
Identity: opts.As,
CheckError: checkErr,
}
if resp.StatusCode >= 400 {
responseErr := client.HandleResponse(resp, responseOpts)
responseErr = imcontract.NormalizeHTTPError(
resp.StatusCode,
resp.Header.Get("x-tt-logid"),
responseErr,
)
return session.FinalizeError(responseErr)
}
parsed, err := parseIMContractJSONResponse(resp)
if err != nil {
return session.FinalizeError(err)
}
if apiErr := checkErr(parsed, opts.As); apiErr != nil {
return session.FinalizeError(apiErr)
}
data := output.SuccessEnvelopeData(parsed)
if m, ok := data.(map[string]any); ok {
session.ObserveResponse(m)
}
result, err := session.FinalizeSuccess(data)
if err != nil {
return err
}
emitErr := emitIMServiceResult(
opts,
format,
result.Data,
result.OK,
nil,
nil,
result.Hint,
false,
)
if emitErr != nil {
if errs.IsContentSafety(emitErr) {
return writeIMContentSafetyFallback(opts, result)
}
if opts.JqExpr != "" {
writeIMJQDiagnostic(opts.Factory.IOStreams.ErrOut)
return writeIMJQFallback(opts, result)
}
return emitErr
}
if result.ExitCode != 0 {
return output.PartialFailure(result.ExitCode)
}
return nil
}
func parseIMContractJSONResponse(resp *larkcore.ApiResp) (interface{}, error) {
if resp == nil {
return nil, newIMContractJSONResponseError(resp)
}
parsed, err := client.ParseJSONResponse(resp)
if err != nil {
return nil, newIMContractJSONResponseError(resp)
}
return parsed, nil
}
func newIMContractJSONResponseError(resp *larkcore.ApiResp) *errs.InternalError {
contractErr := errs.NewInternalError(
errs.SubtypeInvalidResponse,
"IM contract response must be valid JSON",
)
if resp == nil {
return contractErr
}
if logID := resp.Header.Get("x-tt-logid"); logID != "" {
contractErr.WithLogID(logID)
}
return contractErr
}
func normalizeIMContractJSONError(err error) error {
problem, ok := errs.ProblemOf(err)
if ok && problem.Subtype == errs.SubtypeInvalidResponse {
normalized := newIMContractJSONResponseError(nil)
if problem.Code != 0 {
normalized.WithCode(problem.Code)
}
if problem.LogID != "" {
normalized.WithLogID(problem.LogID)
}
return normalized
}
return err
}
func writeIMJQFallback(opts *ServiceMethodOptions, result imcontract.Result) error {
env, signal := imcontract.BuildJQOutputFallback(result)
if err := newIMServiceEmitter(opts).RedactedFallback(env); err != nil {
return err
}
return signal
}
func writeIMJQDiagnostic(errOut io.Writer) {
fmt.Fprintln(errOut, "error: jq projection failed after the IM write completed; inspect --jq")
}
func writeIMContentSafetyFallback(opts *ServiceMethodOptions, result imcontract.Result) error {
env, signal := imcontract.BuildContentSafetyOutputFallback(result)
if err := newIMServiceEmitter(opts).RedactedFallback(env); err != nil {
return err
}
return signal
}
// checkServiceScopes pre-checks user scopes before making the API call.
func checkServiceScopes(ctx context.Context, cred *credential.CredentialProvider, identity core.Identity, config *core.CliConfig, method meta.Method) error {
if ctx.Err() != nil {
@@ -679,6 +1162,13 @@ func serviceDryRunOutputOptions(f *cmdutil.Factory, opts *ServiceMethodOptions)
Identity: opts.As,
Out: f.IOStreams.Out,
ErrOut: f.IOStreams.ErrOut,
NoticeProvider: func() map[string]interface{} {
base := output.GetNotice()
if !opts.identityDefaulted {
return base
}
return imcontract.WithIdentityDefaultedNotice(base, string(opts.As))
},
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,98 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package affordance
import (
"encoding/json"
"os"
"strings"
"testing"
)
// The 21 im raw-API methods that affordance/im.md must cover: 17 first-batch
// methods plus 4 "prefer the shortcut" entries. Keys follow the parsed heading
// form (spaces become dots), same as TestFor's fixture keys.
var imAffordanceMethods = []string{
"chat.members.create", "chat.members.delete", "chat.members.get", "chat.members.bots",
"messages.forward", "messages.delete", "messages.merge_forward", "messages.read_users",
"reactions.create", "reactions.delete", "reactions.list", "reactions.batch_query",
"pins.create", "pins.delete", "pins.list",
"images.create",
"threads.forward",
"chats.get", "chats.update", "chats.create", "chats.link",
}
type parsedAffordance struct {
UseWhen []string `json:"use_when"`
AvoidWhen []string `json:"avoid_when"`
Prerequisites []string `json:"prerequisites"`
Examples []struct {
Command string `json:"command"`
} `json:"examples"`
}
// TestForIMRealFile parses the real affordance/im.md through the production
// parser and asserts coverage plus depth on the showcase method.
func TestForIMRealFile(t *testing.T) {
prev := mdSource
t.Cleanup(func() { SetSource(prev) })
SetSource(os.DirFS("../../affordance"))
for _, m := range imAffordanceMethods {
raw, ok := For("im", m)
if !ok {
t.Errorf("For(\"im\", %q) ok=false, want an overlay section in affordance/im.md", m)
continue
}
var a parsedAffordance
if err := json.Unmarshal(raw, &a); err != nil {
t.Errorf("%s: overlay is not valid affordance JSON: %v", m, err)
continue
}
if len(a.UseWhen) == 0 {
t.Errorf("%s: missing lead paragraph (use_when)", m)
}
if len(a.AvoidWhen) == 0 {
t.Errorf("%s: missing Avoid when section", m)
}
if len(a.Examples) == 0 || a.Examples[0].Command == "" {
t.Errorf("%s: missing fenced example command", m)
continue
}
// Each example must invoke the section's own command, so a heading
// can't silently drift apart from the command its examples show.
// Normalize the example's command words (before the first flag) the
// same way headings become keys: spaces join with dots.
words := strings.Fields(strings.TrimPrefix(a.Examples[0].Command, "lark-cli im "))
var cmdWords []string
for _, w := range words {
if strings.HasPrefix(w, "-") {
break
}
cmdWords = append(cmdWords, w)
}
if got := strings.Join(cmdWords, "."); got != m {
t.Errorf("%s: first example %q invokes %q, want the section's own command", m, a.Examples[0].Command, got)
}
}
// Showcase depth: messages forward (the deepest overlay section).
raw, ok := For("im", "messages.forward")
if !ok {
t.Fatal("messages.forward overlay missing")
}
var fwd parsedAffordance
if err := json.Unmarshal(raw, &fwd); err != nil {
t.Fatalf("messages.forward overlay invalid: %v", err)
}
if len(fwd.AvoidWhen) < 3 {
t.Errorf("messages.forward: want >=3 avoid_when entries, got %d", len(fwd.AvoidWhen))
}
if len(fwd.Prerequisites) < 2 {
t.Errorf("messages.forward: want >=2 prerequisites, got %d", len(fwd.Prerequisites))
}
if len(fwd.Examples) < 1 || fwd.Examples[0].Command == "" {
t.Errorf("messages.forward: want >=1 fenced example command")
}
}

View File

@@ -13,9 +13,10 @@ import (
// PaginationOptions contains pagination control options.
type PaginationOptions struct {
PageLimit int // max pages to fetch; 0 = unlimited (default: 10)
PageDelay int // ms, default 200
Identity core.Identity // identity passed to checkErr; defaults to AsUser when empty
PageLimit int // max pages to fetch; 0 = unlimited (default: 10)
PageDelay int // ms, default 200
Identity core.Identity // identity passed to checkErr; defaults to AsUser when empty
NormalizeHTTPError func(status int, logID string, err error) error
}
func mergePagedResults(w io.Writer, results []interface{}) interface{} {

View File

@@ -0,0 +1,305 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package client
import (
"context"
"io"
"time"
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/core"
)
// StopReason describes the neutral fact that stopped a pagination attempt.
// Business domains decide whether a given reason means success or failure.
type StopReason string
const (
StopReasonExhausted StopReason = "exhausted"
StopReasonSinglePage StopReason = "single_page"
StopReasonPageLimit StopReason = "page_limit"
StopReasonStartPageToken StopReason = "start_page_token"
StopReasonTransportError StopReason = "transport_error"
StopReasonAPIError StopReason = "api_error"
StopReasonMissingToken StopReason = "missing_token"
StopReasonRepeatedToken StopReason = "repeated_token"
StopReasonServerTruncation StopReason = "server_truncation"
)
// PaginationStatus contains pagination facts without interpreting completeness.
// Cause is process-local diagnostic context and must never be serialized.
type PaginationStatus struct {
PagesFetched int `json:"pages_fetched,omitempty"`
HasMore bool `json:"has_more,omitempty"`
NextPageToken string `json:"next_page_token,omitempty"`
StopReason StopReason `json:"stop_reason,omitempty"`
Cause error `json:"-"`
}
// InspectPaginationPage derives status from one already-fetched page.
// It is useful for callers that intentionally perform a single-page read.
func InspectPaginationPage(result interface{}, startPageToken string) (PaginationStatus, error) {
status := PaginationStatus{PagesFetched: 1}
hasMore, nextToken, truncated := paginationFacts(result)
status.HasMore = hasMore
status.NextPageToken = nextToken
if truncated {
status.StopReason = StopReasonServerTruncation
return status, nil
}
if hasMore && nextToken == "" {
err := missingPaginationTokenError()
status.StopReason = StopReasonMissingToken
status.Cause = err
return status, err
}
if hasMore && startPageToken != "" && nextToken == startPageToken {
err := repeatedPaginationTokenError()
status.StopReason = StopReasonRepeatedToken
status.Cause = err
return status, err
}
if startPageToken != "" {
status.StopReason = StopReasonStartPageToken
return status, nil
}
if hasMore {
status.StopReason = StopReasonSinglePage
return status, nil
}
status.StopReason = StopReasonExhausted
return status, nil
}
// PaginateAllWithStatus fetches pages until a neutral stop condition occurs.
// Unlike PaginateAll, later failures are returned together with already-fetched
// data so an opt-in caller can report an incomplete result without losing it.
func (c *APIClient) PaginateAllWithStatus(
ctx context.Context,
request *RawApiRequest,
opts PaginationOptions,
) (map[string]interface{}, PaginationStatus, error) {
results, status, err := c.paginateLoopWithStatus(ctx, request, opts, nil)
return mergeStatusResults(io.Discard, results), status, err
}
// StreamPagesWithStatus emits each successful raw page and returns the neutral
// stop status. A later failure does not retract pages already emitted.
func (c *APIClient) StreamPagesWithStatus(
ctx context.Context,
request *RawApiRequest,
opts PaginationOptions,
emit func(page map[string]interface{}) error,
) (PaginationStatus, error) {
_, status, err := c.paginateLoopWithStatus(ctx, request, opts, emit)
return status, err
}
func (c *APIClient) paginateLoopWithStatus(
ctx context.Context,
request *RawApiRequest,
opts PaginationOptions,
emit func(page map[string]interface{}) error,
) ([]interface{}, PaginationStatus, error) {
if request == nil {
err := errs.NewInternalError(errs.SubtypeInvalidResponse, "pagination request is nil")
return nil, PaginationStatus{Cause: err}, err
}
var results []interface{}
status := PaginationStatus{}
nextToken := stringParam(request.Params, "page_token")
startPageToken := nextToken
seenTokens := make(map[string]struct{})
if nextToken != "" {
seenTokens[nextToken] = struct{}{}
}
pageDelay := opts.PageDelay
if pageDelay == 0 {
pageDelay = 200
}
for {
params := cloneParams(request.Params)
if nextToken != "" {
params["page_token"] = nextToken
}
resp, err := c.DoAPI(ctx, RawApiRequest{
Method: request.Method,
URL: request.URL,
Params: params,
Data: request.Data,
As: request.As,
ExtraOpts: request.ExtraOpts,
})
if err != nil {
status.StopReason = StopReasonTransportError
status.Cause = err
status.HasMore = nextToken != ""
status.NextPageToken = nextToken
return results, status, err
}
result, err := ParseJSONResponse(resp)
if err != nil {
err = WrapJSONResponseParseError(err, resp.RawBody)
if opts.NormalizeHTTPError != nil && resp.StatusCode >= 400 {
err = opts.NormalizeHTTPError(resp.StatusCode, streamLogID(resp.Header), err)
}
status.StopReason = StopReasonTransportError
status.Cause = err
status.HasMore = nextToken != ""
status.NextPageToken = nextToken
return results, status, err
}
identity := opts.Identity
if identity == "" {
identity = request.As
}
if identity == "" {
identity = core.AsUser
}
apiErr := c.CheckResponse(result, identity)
if opts.NormalizeHTTPError != nil && resp.StatusCode >= 400 {
apiErr = opts.NormalizeHTTPError(resp.StatusCode, streamLogID(resp.Header), apiErr)
}
if apiErr != nil {
status.StopReason = StopReasonAPIError
status.Cause = apiErr
status.HasMore = nextToken != ""
status.NextPageToken = nextToken
return results, status, apiErr
}
page, ok := result.(map[string]interface{})
if !ok {
err := errs.NewInternalError(errs.SubtypeInvalidResponse, "pagination response must be a JSON object")
status.StopReason = StopReasonAPIError
status.Cause = err
return results, status, err
}
results = append(results, result)
status.PagesFetched++
if emit != nil {
if err := emit(page); err != nil {
status.Cause = err
return results, status, err
}
}
hasMore, returnedToken, truncated := paginationFacts(result)
status.HasMore = hasMore
status.NextPageToken = returnedToken
if truncated {
status.StopReason = StopReasonServerTruncation
return results, status, nil
}
if !hasMore {
if startPageToken != "" {
status.StopReason = StopReasonStartPageToken
} else {
status.StopReason = StopReasonExhausted
}
status.NextPageToken = ""
return results, status, nil
}
if returnedToken == "" {
err := missingPaginationTokenError()
status.StopReason = StopReasonMissingToken
status.Cause = err
return results, status, err
}
if _, exists := seenTokens[returnedToken]; exists {
err := repeatedPaginationTokenError()
status.StopReason = StopReasonRepeatedToken
status.Cause = err
return results, status, err
}
if opts.PageLimit > 0 && status.PagesFetched >= opts.PageLimit {
status.StopReason = StopReasonPageLimit
return results, status, nil
}
seenTokens[returnedToken] = struct{}{}
nextToken = returnedToken
if pageDelay > 0 {
time.Sleep(time.Duration(pageDelay) * time.Millisecond)
}
}
}
func paginationFacts(result interface{}) (hasMore bool, nextToken string, truncated bool) {
resultMap, ok := result.(map[string]interface{})
if !ok {
return false, "", false
}
truncated = explicitTruncation(resultMap)
data, ok := resultMap["data"].(map[string]interface{})
if !ok {
return false, "", truncated
}
hasMore, _ = data["has_more"].(bool)
nextToken = stringParam(data, "page_token")
if nextToken == "" {
nextToken = stringParam(data, "next_page_token")
}
return hasMore, nextToken, truncated || explicitTruncation(data)
}
func explicitTruncation(object map[string]interface{}) bool {
truncated, _ := object["truncated"].(bool)
isTruncated, _ := object["is_truncated"].(bool)
return truncated || isTruncated
}
func stringParam(params map[string]interface{}, name string) string {
value, _ := params[name].(string)
return value
}
func cloneParams(params map[string]interface{}) map[string]interface{} {
cloned := make(map[string]interface{}, len(params)+1)
for key, value := range params {
cloned[key] = value
}
return cloned
}
func missingPaginationTokenError() error {
return errs.NewInternalError(
errs.SubtypeInvalidResponse,
"paginated response has_more=true but next page token is missing",
)
}
func repeatedPaginationTokenError() error {
return errs.NewInternalError(
errs.SubtypeInvalidResponse,
"paginated response repeated the same next page token",
)
}
func mergeStatusResults(w io.Writer, results []interface{}) map[string]interface{} {
if len(results) == 0 {
return map[string]interface{}{}
}
if len(results) == 1 {
if result, ok := results[0].(map[string]interface{}); ok {
return result
}
return map[string]interface{}{"pages": results}
}
if w == nil {
w = io.Discard
}
merged := mergePagedResults(w, results)
if result, ok := merged.(map[string]interface{}); ok {
return result
}
return map[string]interface{}{"pages": results}
}

View File

@@ -0,0 +1,451 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package client
import (
"context"
"encoding/json"
"errors"
"net"
"net/http"
"strings"
"testing"
"github.com/larksuite/cli/errs"
)
func TestInspectPaginationPageStatus(t *testing.T) {
tests := []struct {
name string
data map[string]interface{}
startToken string
want StopReason
wantMore bool
wantToken string
wantErr bool
}{
{
name: "exhausted",
data: map[string]interface{}{"has_more": false},
want: StopReasonExhausted,
},
{
name: "single page",
data: map[string]interface{}{"has_more": true, "page_token": "next"},
want: StopReasonSinglePage,
wantMore: true,
wantToken: "next",
},
{
name: "start page token",
data: map[string]interface{}{"has_more": false},
startToken: "middle",
want: StopReasonStartPageToken,
},
{
name: "missing token",
data: map[string]interface{}{"has_more": true},
want: StopReasonMissingToken,
wantMore: true,
wantErr: true,
},
{
name: "server truncation",
data: map[string]interface{}{"has_more": false, "truncated": true},
want: StopReasonServerTruncation,
},
{
name: "message text does not imply server truncation",
data: map[string]interface{}{"has_more": false, "message": "result was truncated"},
want: StopReasonExhausted,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := map[string]interface{}{
"code": float64(0),
"data": tt.data,
}
status, err := InspectPaginationPage(result, tt.startToken)
if (err != nil) != tt.wantErr {
t.Fatalf("InspectPaginationPage() error = %v, wantErr %v", err, tt.wantErr)
}
if status.StopReason != tt.want {
t.Errorf("StopReason = %q, want %q", status.StopReason, tt.want)
}
if status.PagesFetched != 1 {
t.Errorf("PagesFetched = %d, want 1", status.PagesFetched)
}
if status.HasMore != tt.wantMore {
t.Errorf("HasMore = %v, want %v", status.HasMore, tt.wantMore)
}
if status.NextPageToken != tt.wantToken {
t.Errorf("NextPageToken = %q, want %q", status.NextPageToken, tt.wantToken)
}
if status.Cause != err {
t.Errorf("Cause = %v, want returned error %v", status.Cause, err)
}
})
}
}
func TestPaginationStatusCauseIsNotSerialized(t *testing.T) {
status := PaginationStatus{
PagesFetched: 1,
HasMore: true,
NextPageToken: "next",
StopReason: StopReasonTransportError,
Cause: errors.New("contains sensitive transport details"),
}
raw, err := json.Marshal(status)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
if strings.Contains(string(raw), "sensitive") || strings.Contains(string(raw), "cause") {
t.Fatalf("serialized status leaked Cause: %s", raw)
}
}
func TestPaginateAllWithStatusStopReasons(t *testing.T) {
tests := []struct {
name string
firstToken string
pageLimit int
pages []map[string]interface{}
wantCalls int
wantReason StopReason
wantPages int
wantMore bool
wantToken string
wantErr bool
}{
{
name: "exhausted with unlimited page limit",
pages: []map[string]interface{}{
pageResult(true, "next", false, "1"),
pageResult(false, "", false, "2"),
},
wantCalls: 2,
wantReason: StopReasonExhausted,
wantPages: 2,
},
{
name: "page limit",
pages: []map[string]interface{}{
pageResult(true, "next", false, "1"),
pageResult(true, "last", false, "2"),
},
pageLimit: 2,
wantCalls: 2,
wantReason: StopReasonPageLimit,
wantPages: 2,
wantMore: true,
wantToken: "last",
},
{
name: "start page token stays incomplete after exhaustion",
firstToken: "middle",
pages: []map[string]interface{}{
pageResult(false, "", false, "1"),
},
wantCalls: 1,
wantReason: StopReasonStartPageToken,
wantPages: 1,
},
{
name: "missing token fails closed",
pages: []map[string]interface{}{
pageResult(true, "", false, "1"),
},
wantCalls: 1,
wantReason: StopReasonMissingToken,
wantPages: 1,
wantMore: true,
wantErr: true,
},
{
name: "repeated token fails closed",
pages: []map[string]interface{}{
pageResult(true, "secret-token-x", false, "1"),
pageResult(true, "secret-token-x", false, "2"),
},
wantCalls: 2,
wantReason: StopReasonRepeatedToken,
wantPages: 2,
wantMore: true,
wantToken: "secret-token-x",
wantErr: true,
},
{
name: "server truncation is explicit structured fact",
pages: []map[string]interface{}{
pageResult(false, "", true, "1"),
},
wantCalls: 1,
wantReason: StopReasonServerTruncation,
wantPages: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
calls := 0
ac, _ := newTestAPIClient(t, roundTripFunc(func(_ *http.Request) (*http.Response, error) {
if calls >= len(tt.pages) {
t.Fatalf("unexpected API call %d", calls+1)
}
body := tt.pages[calls]
calls++
return jsonResponse(body), nil
}))
params := map[string]interface{}{}
if tt.firstToken != "" {
params["page_token"] = tt.firstToken
}
result, status, err := ac.PaginateAllWithStatus(context.Background(), &RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
Params: params,
As: "bot",
}, PaginationOptions{PageLimit: tt.pageLimit, PageDelay: -1})
if (err != nil) != tt.wantErr {
t.Fatalf("PaginateAllWithStatus() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
switch tt.wantReason {
case StopReasonMissingToken:
if err.Error() != "paginated response has_more=true but next page token is missing" {
t.Fatalf("missing-token error = %q", err)
}
case StopReasonRepeatedToken:
if err.Error() != "paginated response repeated the same next page token" {
t.Fatalf("repeated-token error = %q", err)
}
}
}
if calls != tt.wantCalls {
t.Errorf("API calls = %d, want %d", calls, tt.wantCalls)
}
if status.StopReason != tt.wantReason {
t.Errorf("StopReason = %q, want %q", status.StopReason, tt.wantReason)
}
if status.PagesFetched != tt.wantPages {
t.Errorf("PagesFetched = %d, want %d", status.PagesFetched, tt.wantPages)
}
if status.HasMore != tt.wantMore {
t.Errorf("HasMore = %v, want %v", status.HasMore, tt.wantMore)
}
if status.NextPageToken != tt.wantToken {
t.Errorf("NextPageToken = %q, want %q", status.NextPageToken, tt.wantToken)
}
if result == nil {
t.Fatal("result must preserve successfully fetched pages")
}
if tt.wantErr {
var internalErr *errs.InternalError
if !errors.As(err, &internalErr) || internalErr.Subtype != errs.SubtypeInvalidResponse {
t.Fatalf("error = %T %v, want invalid_response InternalError", err, err)
}
if tt.wantToken != "" && strings.Contains(err.Error(), tt.wantToken) {
t.Fatalf("error leaked page token: %v", err)
}
}
})
}
}
func TestPaginateAllWithStatusPreservesPartialResultAndTypedLateError(t *testing.T) {
t.Run("transport error", func(t *testing.T) {
calls := 0
ac, _ := newTestAPIClient(t, roundTripFunc(func(_ *http.Request) (*http.Response, error) {
calls++
if calls == 1 {
return jsonResponse(pageResult(true, "next", false, "1")), nil
}
return nil, &net.DNSError{Err: "no such host", Name: "example.invalid"}
}))
result, status, err := ac.PaginateAllWithStatus(context.Background(), &RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
As: "bot",
}, PaginationOptions{PageDelay: -1})
var networkErr *errs.NetworkError
if !errors.As(err, &networkErr) {
t.Fatalf("error = %T %v, want typed NetworkError", err, err)
}
assertPartialPage(t, result, "1")
if status.StopReason != StopReasonTransportError || status.PagesFetched != 1 || status.NextPageToken != "next" {
t.Fatalf("status = %#v, want late transport error with resumable token", status)
}
if status.Cause != err {
t.Fatalf("Cause = %v, want returned error %v", status.Cause, err)
}
})
t.Run("API error", func(t *testing.T) {
calls := 0
ac, _ := newTestAPIClient(t, roundTripFunc(func(_ *http.Request) (*http.Response, error) {
calls++
if calls == 1 {
return jsonResponse(pageResult(true, "next", false, "1")), nil
}
return jsonResponse(map[string]interface{}{"code": 999, "msg": "failed"}), nil
}))
result, status, err := ac.PaginateAllWithStatus(context.Background(), &RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
As: "bot",
}, PaginationOptions{PageDelay: -1})
var apiErr *errs.APIError
if !errors.As(err, &apiErr) {
t.Fatalf("error = %T %v, want typed APIError", err, err)
}
assertPartialPage(t, result, "1")
if status.StopReason != StopReasonAPIError || status.PagesFetched != 1 || status.NextPageToken != "next" {
t.Fatalf("status = %#v, want late API error with resumable token", status)
}
})
}
func TestPaginateAllWithStatusHTTPNormalizerIsOptIn(t *testing.T) {
newClient := func(t *testing.T) *APIClient {
t.Helper()
response := jsonResponse(pageResult(false, "", false, "1"))
response.StatusCode = http.StatusServiceUnavailable
ac, _ := newTestAPIClient(t, roundTripFunc(func(_ *http.Request) (*http.Response, error) {
return response, nil
}))
return ac
}
t.Run("normalizer classifies HTTP status", func(t *testing.T) {
marker := errors.New("normalized HTTP failure")
ac := newClient(t)
_, status, err := ac.PaginateAllWithStatus(context.Background(), &RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
As: "bot",
}, PaginationOptions{
PageDelay: -1,
NormalizeHTTPError: func(status int, _ string, err error) error {
if status != http.StatusServiceUnavailable || err != nil {
t.Fatalf("normalizer input = status %d, err %v", status, err)
}
return marker
},
})
if !errors.Is(err, marker) || status.StopReason != StopReasonAPIError {
t.Fatalf("err = %v, status = %#v", err, status)
}
})
t.Run("nil normalizer preserves legacy behavior", func(t *testing.T) {
ac := newClient(t)
_, status, err := ac.PaginateAllWithStatus(context.Background(), &RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
As: "bot",
}, PaginationOptions{PageDelay: -1})
if err != nil || status.StopReason != StopReasonExhausted {
t.Fatalf("err = %v, status = %#v", err, status)
}
})
}
func TestStreamPagesWithStatusPreservesEmittedPagesOnLateError(t *testing.T) {
calls := 0
ac, _ := newTestAPIClient(t, roundTripFunc(func(_ *http.Request) (*http.Response, error) {
calls++
if calls == 1 {
return jsonResponse(pageResult(true, "next", false, "1")), nil
}
return nil, &net.DNSError{Err: "no such host", Name: "example.invalid"}
}))
var emitted []map[string]interface{}
status, err := ac.StreamPagesWithStatus(context.Background(), &RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
As: "bot",
}, PaginationOptions{PageDelay: -1}, func(page map[string]interface{}) error {
emitted = append(emitted, page)
return nil
})
var networkErr *errs.NetworkError
if !errors.As(err, &networkErr) {
t.Fatalf("error = %T %v, want typed NetworkError", err, err)
}
if len(emitted) != 1 {
t.Fatalf("emitted pages = %d, want 1", len(emitted))
}
if status.StopReason != StopReasonTransportError || status.PagesFetched != 1 {
t.Fatalf("status = %#v, want late transport error", status)
}
}
func TestLegacyPaginateAllStillSwallowsLateTransportError(t *testing.T) {
calls := 0
ac, errOut := newTestAPIClient(t, roundTripFunc(func(_ *http.Request) (*http.Response, error) {
calls++
if calls == 1 {
return jsonResponse(pageResult(true, "next", false, "1")), nil
}
return nil, &net.DNSError{Err: "no such host", Name: "example.invalid"}
}))
result, err := ac.PaginateAll(context.Background(), RawApiRequest{
Method: "GET",
URL: "/open-apis/test",
As: "bot",
}, PaginationOptions{PageDelay: -1})
if err != nil {
t.Fatalf("legacy PaginateAll() error = %v, want nil", err)
}
assertPartialPage(t, result, "1")
if !strings.Contains(errOut.String(), "[page 2] error, stopping pagination") {
t.Fatalf("legacy warning changed: %q", errOut.String())
}
}
func pageResult(hasMore bool, token string, truncated bool, id string) map[string]interface{} {
data := map[string]interface{}{
"items": []interface{}{map[string]interface{}{"id": id}},
"has_more": hasMore,
"truncated": truncated,
}
if token != "" {
data["page_token"] = token
}
return map[string]interface{}{"code": float64(0), "msg": "ok", "data": data}
}
func assertPartialPage(t *testing.T, result interface{}, wantID string) {
t.Helper()
resultMap, ok := result.(map[string]interface{})
if !ok {
t.Fatalf("result = %T, want map", result)
}
data, ok := resultMap["data"].(map[string]interface{})
if !ok {
t.Fatalf("data = %T, want map", resultMap["data"])
}
items, ok := data["items"].([]interface{})
if !ok || len(items) != 1 {
t.Fatalf("items = %#v, want one item", data["items"])
}
item, ok := items[0].(map[string]interface{})
if !ok || item["id"] != wantID {
t.Fatalf("item = %#v, want id %q", items[0], wantID)
}
}

View File

@@ -29,6 +29,9 @@ type DryRunOutputOptions struct {
Identity core.Identity
Out io.Writer
ErrOut io.Writer
// NoticeProvider is optional. Nil preserves the process-wide notice source;
// command-specific callers can merge invocation facts without mutating it.
NoticeProvider output.NoticeProvider
}
// DryRunAPICall describes a single API call in dry-run output.
@@ -306,12 +309,20 @@ func WriteDryRun(dr *DryRunAPI, opts DryRunOutputOptions) error {
fmt.Fprint(opts.Out, dr.Format())
return nil
}
return output.WriteSuccessEnvelope(dr, output.SuccessEnvelopeOptions{
CommandPath: opts.CommandPath,
Identity: string(opts.Identity),
DryRun: true,
JqExpr: opts.JqExpr,
Out: opts.Out,
ErrOut: opts.ErrOut,
noticeProvider := opts.NoticeProvider
if noticeProvider == nil {
noticeProvider = output.GetNotice
}
return output.NewEmitter(output.EmitterConfig{
Out: opts.Out,
ErrOut: opts.ErrOut,
CommandPath: opts.CommandPath,
Identity: string(opts.Identity),
NoticeProvider: noticeProvider,
}).Success(dr, output.EmitOptions{
Format: "",
JQ: opts.JqExpr,
DryRun: true,
JQSafetyWarning: true,
})
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/client"
"github.com/larksuite/cli/internal/core"
"github.com/larksuite/cli/internal/output"
)
func TestDryRunAPI_SingleGET(t *testing.T) {
@@ -193,6 +194,33 @@ func TestPrintDryRun_JSON(t *testing.T) {
}
}
func TestPrintDryRun_JSONUsesCommandScopedNoticeProvider(t *testing.T) {
var buf bytes.Buffer
err := PrintDryRun(client.RawApiRequest{
Method: "POST",
URL: "/open-apis/test",
As: "bot",
}, &core.CliConfig{AppID: "app123"}, DryRunOutputOptions{
Format: "json",
Identity: core.AsBot,
Out: &buf,
ErrOut: io.Discard,
NoticeProvider: func() map[string]interface{} {
return map[string]interface{}{"identity_defaulted": map[string]interface{}{"resolved": "bot"}}
},
})
if err != nil {
t.Fatalf("PrintDryRun failed: %v", err)
}
var env output.Envelope
if err := json.Unmarshal(buf.Bytes(), &env); err != nil {
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, buf.String())
}
if got := env.Notice["identity_defaulted"].(map[string]interface{})["resolved"]; got != "bot" {
t.Fatalf("identity_defaulted.resolved = %#v", got)
}
}
func TestPrintDryRun_Pretty(t *testing.T) {
var buf bytes.Buffer
var errBuf bytes.Buffer

View File

@@ -4,6 +4,8 @@
package cmdutil
import (
"fmt"
"github.com/larksuite/cli/internal/core"
"github.com/spf13/cobra"
)
@@ -43,3 +45,15 @@ func GetRisk(cmd *cobra.Command) (level string, ok bool) {
level, ok = cmd.Annotations[riskLevelAnnotationKey]
return level, ok && level != ""
}
// RiskHelpText returns the canonical help line for a risk level. High-risk
// writes retain the confirmation boundary wherever the line is rendered.
func RiskHelpText(level string) string {
if level == RiskHighRiskWrite {
return fmt.Sprintf(
"Risk: %s (requires explicit user confirmation to execute; the agent must NOT add --yes on its own — only pass --yes after the user has confirmed)",
level,
)
}
return fmt.Sprintf("Risk: %s", level)
}

View File

@@ -4,11 +4,24 @@
package cmdutil
import (
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestRiskHelpTextPreservesHighRiskConfirmationGuard(t *testing.T) {
if got := RiskHelpText(RiskWrite); got != "Risk: write" {
t.Fatalf("RiskHelpText(write) = %q", got)
}
got := RiskHelpText(RiskHighRiskWrite)
for _, want := range []string{"Risk: high-risk-write", "requires explicit user confirmation", "agent must NOT add --yes"} {
if !strings.Contains(got, want) {
t.Fatalf("RiskHelpText(high-risk-write) missing %q: %q", want, got)
}
}
}
func TestSetRisk_EmptyLevelShortCircuits(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
SetRisk(cmd, "")

View File

@@ -26,6 +26,7 @@ const (
HeaderShortcut = "X-Cli-Shortcut"
HeaderExecutionId = "X-Cli-Execution-Id"
HeaderAgentTrace = "X-Agent-Trace"
HeaderAgentName = "X-Agent-Name"
SourceValue = "lark-cli"
@@ -55,6 +56,9 @@ func BaseSecurityHeaders() http.Header {
if v := envvars.AgentTrace(); v != "" {
h.Set(HeaderAgentTrace, v)
}
if v := envvars.AgentName(); v != "" {
h.Set(HeaderAgentName, v)
}
return h
}

View File

@@ -263,9 +263,34 @@ func TestBaseSecurityHeaders_AllRequiredHeaders(t *testing.T) {
}
// ---------------------------------------------------------------------------
// HeaderAgentTrace injection (via BaseSecurityHeaders)
// Agent headers injected via BaseSecurityHeaders
// ---------------------------------------------------------------------------
func TestBaseSecurityHeaders_NoAgentNameHeaderWhenEnvUnset(t *testing.T) {
t.Setenv(envvars.CliAgentName, "")
h := BaseSecurityHeaders()
if v := h.Get(HeaderAgentName); v != "" {
t.Fatalf("BaseSecurityHeaders() included %s = %q, want absent when env unset", HeaderAgentName, v)
}
}
func TestBaseSecurityHeaders_IncludesAgentNameHeaderWhenEnvSet(t *testing.T) {
const agentName = "sample-agent"
t.Setenv(envvars.CliAgentName, agentName)
h := BaseSecurityHeaders()
if v := h.Get(HeaderAgentName); v != agentName {
t.Fatalf("BaseSecurityHeaders()[%s] = %q, want %q", HeaderAgentName, v, agentName)
}
}
func TestBaseSecurityHeaders_NoAgentNameHeaderWhenEnvInvalid(t *testing.T) {
t.Setenv(envvars.CliAgentName, "agent\r\nX-Evil: attack")
h := BaseSecurityHeaders()
if v := h.Get(HeaderAgentName); v != "" {
t.Fatalf("BaseSecurityHeaders() included %s = %q, want absent for invalid input", HeaderAgentName, v)
}
}
func TestBaseSecurityHeaders_NoAgentTraceHeaderWhenEnvUnset(t *testing.T) {
t.Setenv(envvars.CliAgentTrace, "")
h := BaseSecurityHeaders()

View File

@@ -16,16 +16,18 @@ func TestAgentName_EmptyWhenEnvUnset(t *testing.T) {
}
func TestAgentName_ReturnsCleanValue(t *testing.T) {
t.Setenv(CliAgentName, "claude-code")
if got := AgentName(); got != "claude-code" {
t.Fatalf("AgentName() = %q, want %q", got, "claude-code")
const agentName = "sample-agent"
t.Setenv(CliAgentName, agentName)
if got := AgentName(); got != agentName {
t.Fatalf("AgentName() = %q, want %q", got, agentName)
}
}
func TestAgentName_TrimsWhitespace(t *testing.T) {
t.Setenv(CliAgentName, " cursor ")
if got := AgentName(); got != "cursor" {
t.Fatalf("AgentName() = %q, want %q (whitespace trimmed)", got, "cursor")
const agentName = "sample-agent"
t.Setenv(CliAgentName, " "+agentName+" ")
if got := AgentName(); got != agentName {
t.Fatalf("AgentName() = %q, want %q (whitespace trimmed)", got, agentName)
}
}

View File

@@ -38,6 +38,10 @@ type Stub struct {
// matches after the first hit. Each match appends to CapturedBodies.
Reusable bool
// Optional (optional): when true, Verify does not require this stub to be
// matched. Useful for negative assertions via OnMatch.
Optional bool
// CapturedHeaders records the request headers of the matched request.
// Populated after RoundTrip matches this stub.
CapturedHeaders http.Header
@@ -137,6 +141,9 @@ func (r *Registry) Verify(t testing.TB) {
if s.matched {
continue
}
if s.Optional {
continue
}
// Reusable stubs never set s.matched; treat any captured hit as a match.
if s.Reusable && len(s.CapturedBodies) > 0 {
continue

View File

@@ -0,0 +1,301 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package catalog
import (
"fmt"
"sort"
)
func ack(key string) Contract {
return Contract{Key: ContractKey(key), Strategy: Strategy{Kind: AuthoritativeAckKind}, ReplayMode: ReplayForbidden}
}
func required(key string, result RequiredSpec, replay ReplayMode) Contract {
return Contract{
Key: ContractKey(key),
Strategy: Strategy{Kind: RequiredResultKind, Required: result},
ReplayMode: replay,
}
}
func batch(key string, request EvidenceSpec, failures ...EvidenceSpec) Contract {
return Contract{
Key: ContractKey(key),
PartialRecovery: PartialRecoveryFailedItemsOnly,
Strategy: Strategy{
Kind: BatchPartialKind,
Request: request,
Failures: failures,
},
ReplayMode: ReplayForbidden,
}
}
func read(key string, kind StrategyKind) Contract {
return Contract{
Key: ContractKey(key),
Strategy: Strategy{Kind: kind},
}
}
func search(key, collectionField string) Contract {
contract := Contract{
Key: ContractKey(key),
Strategy: Strategy{
Kind: SearchReadKind,
CollectionField: collectionField,
},
}
if key == "im +messages-search" {
contract.Strategy.RequiresMaterialization = true
}
return contract
}
func topString(field string) RequiredSpec {
return RequiredSpec{Shape: RequiredTopString, Field: field}
}
func topObject(field string) RequiredSpec {
return RequiredSpec{Shape: RequiredTopObject, Field: field}
}
func nestedString(field, child string) RequiredSpec {
return RequiredSpec{Shape: RequiredNestedString, Field: field, Child: child}
}
func stringsFrom(field string) EvidenceSpec {
return EvidenceSpec{Shape: EvidenceStrings, Field: field}
}
func objectsFrom(field, idField string) EvidenceSpec {
return EvidenceSpec{Shape: EvidenceObjects, Field: field, IDField: idField}
}
func nestedObjectsFrom(field, container, idField string) EvidenceSpec {
return EvidenceSpec{
Shape: EvidenceNestedObjects, Field: field, Container: container, IDField: idField,
}
}
func feedObjectsFrom(field string) EvidenceSpec {
return EvidenceSpec{Shape: EvidenceFeedObjects, Field: field}
}
func nestedFeedObjectsFrom(field, container string) EvidenceSpec {
return EvidenceSpec{Shape: EvidenceNestedFeedObjects, Field: field, Container: container}
}
func statusObjectsFrom(field, idField string) EvidenceSpec {
return EvidenceSpec{Shape: EvidenceStatusObjects, Field: field, IDField: idField}
}
var contracts = buildContracts()
func buildContracts() map[ContractKey]Contract {
all := []Contract{
read("im +feed-group-query-item", EntityReadKind),
read("im +messages-mget", EntityReadKind),
read("im chat.nickname get", EntityReadKind),
read("im chat.user_setting batch_query", EntityReadKind),
read("im chats get", EntityReadKind),
read("im feed.groups batch_query", EntityReadKind),
func() Contract {
c := read("im reactions batch_query", EntityReadKind)
c.Strategy.ReadHint = HintBatchReactions
return c
}(),
read("im +chat-list", CollectionReadKind),
read("im +chat-members-list", CollectionReadKind),
read("im +chat-messages-list", CollectionReadKind),
read("im +feed-group-list", CollectionReadKind),
read("im +feed-group-list-item", CollectionReadKind),
read("im +feed-shortcut-list", CollectionReadKind),
read("im +flag-list", CollectionReadKind),
read("im +threads-messages-list", CollectionReadKind),
read("im chat.members bots", EntityReadKind),
read("im chat.members get", CollectionReadKind),
read("im chat.moderation get", CollectionReadKind),
read("im messages read_users", CollectionReadKind),
read("im pins list", CollectionReadKind),
read("im reactions list", CollectionReadKind),
search("im +chat-search", "chats"),
search("im +messages-search", "messages"),
read("im +messages-resources-download", MaterializeReadKind),
ack("im +chat-update"),
ack("im +flag-create"),
ack("im chat.nickname delete"),
ack("im chat.nickname update"),
ack("im chats update"),
ack("im feed.groups delete"),
ack("im feed.groups update"),
ack("im messages delete"),
ack("im pins delete"),
required("im +chat-create", topString("chat_id"), ReplaySameIdempotencyKey),
required("im +messages-reply", topString("message_id"), ReplaySameIdempotencyKey),
required("im +messages-send", topString("message_id"), ReplaySameIdempotencyKey),
required("im chats create", topString("chat_id"), ReplaySameIdempotencyKey),
required("im chats link", topString("share_link"), ReplayForbidden),
required("im feed.groups create", topString("group_id"), ReplayForbidden),
required("im images create", topString("image_key"), ReplayForbidden),
required("im messages forward", topString("message_id"), ReplaySameIdempotencyKey),
required("im pins create", topObject("pin"), ReplayForbidden),
required("im reactions create", topString("reaction_id"), ReplayForbidden),
required("im reactions delete", topString("reaction_id"), ReplayForbidden),
required("im threads forward", topString("message_id"), ReplaySameIdempotencyKey),
func() Contract {
c := batch(
"im +feed-shortcut-create",
objectsFrom("shortcuts", "feed_card_id"),
nestedObjectsFrom("failed_shortcuts", "shortcut", "feed_card_id"),
)
c.ReplayMode = ReplaySafe
c.PartialRecovery = PartialRecoveryWholeRequest
return c
}(),
func() Contract {
c := batch(
"im +feed-shortcut-remove",
objectsFrom("shortcuts", "feed_card_id"),
nestedObjectsFrom("failed_shortcuts", "shortcut", "feed_card_id"),
)
c.ReplayMode = ReplaySafe
c.PartialRecovery = PartialRecoveryWholeRequest
return c
}(),
{
Key: "im +flag-cancel",
PartialRecovery: PartialRecoveryWholeRequest,
Strategy: Strategy{
Kind: BatchPartialKind,
ResultLedger: ptrEvidence(statusObjectsFrom("results", "flag_type")),
},
ReplayMode: ReplaySafe,
},
{
Key: "im chat.members create",
Strategy: Strategy{
Kind: BatchPartialKind,
Request: stringsFrom("id_list"),
Failures: []EvidenceSpec{
stringsFrom("invalid_id_list"),
stringsFrom("not_existed_id_list"),
},
Pending: []EvidenceSpec{stringsFrom("pending_approval_id_list")},
},
ReplayMode: ReplayForbidden,
},
batch("im chat.members delete", stringsFrom("id_list"), stringsFrom("invalid_id_list")),
batch(
"im chat.user_setting batch_update",
objectsFrom("chat_settings", "chat_id"),
objectsFrom("invalid_ids", "id"),
),
{
Key: "im feed.groups batch_add_item",
Strategy: Strategy{
Kind: BatchPartialKind,
Request: feedObjectsFrom("items"),
Failures: []EvidenceSpec{nestedFeedObjectsFrom("failed_items", "item")},
},
ReplayMode: ReplayForbidden,
},
{
Key: "im feed.groups batch_remove_item",
Strategy: Strategy{
Kind: BatchPartialKind,
Request: feedObjectsFrom("items"),
Failures: []EvidenceSpec{nestedFeedObjectsFrom("failed_items", "item")},
},
ReplayMode: ReplayForbidden,
},
batch("im messages urgent_app", stringsFrom("user_id_list"), stringsFrom("invalid_user_id_list")),
batch("im messages urgent_phone", stringsFrom("user_id_list"), stringsFrom("invalid_user_id_list")),
batch("im messages urgent_sms", stringsFrom("user_id_list"), stringsFrom("invalid_user_id_list")),
{
Key: "im messages merge_forward",
Strategy: Strategy{
Kind: RequiredResultBatchPartialKind,
Required: nestedString("message", "message_id"),
Request: stringsFrom("message_id_list"),
Failures: []EvidenceSpec{stringsFrom("invalid_message_id_list")},
},
ReplayMode: ReplaySameIdempotencyKey,
},
{
Key: "im chat.managers add_managers",
Strategy: Strategy{
Kind: ResponseSetAssertionKind,
Request: stringsFrom("manager_ids"),
ResponseSets: []EvidenceSpec{stringsFrom("chat_managers"), stringsFrom("chat_bot_managers")},
Assertion: AssertRequestedPresent,
},
ReplayMode: ReplayForbidden,
},
{
Key: "im chat.managers delete_managers",
Strategy: Strategy{
Kind: ResponseSetAssertionKind,
Request: stringsFrom("manager_ids"),
ResponseSets: []EvidenceSpec{stringsFrom("chat_managers"), stringsFrom("chat_bot_managers")},
Assertion: AssertRequestedAbsent,
},
ReplayMode: ReplayForbidden,
},
{
Key: "im chat.moderation update",
Strategy: Strategy{Kind: AcceptanceOnlyKind},
ReplayMode: ReplayForbidden,
},
}
out := make(map[ContractKey]Contract, len(all))
for _, c := range all {
if c.PartialRecovery == "" &&
(c.Strategy.Kind == BatchPartialKind || c.Strategy.Kind == RequiredResultBatchPartialKind) {
c.PartialRecovery = PartialRecoveryFailedItemsOnly
}
switch {
case c.Strategy.Kind == CollectionReadKind || c.Strategy.Kind == SearchReadKind:
c.HelpPolicy = HelpCompleteness
case c.Strategy.Kind == AcceptanceOnlyKind:
c.HelpPolicy = HelpAcceptanceOnly
}
out[c.Key] = c
}
return out
}
func ptrEvidence(spec EvidenceSpec) *EvidenceSpec {
return &spec
}
func Lookup(key ContractKey) (Contract, bool) {
c, ok := contracts[key]
return c, ok
}
func All() []Contract {
out := make([]Contract, 0, len(contracts))
for _, c := range contracts {
out = append(out, c)
}
sort.Slice(out, func(i, j int) bool { return out[i].Key < out[j].Key })
return out
}
func ValidateRegistry() error {
for key, c := range contracts {
if key == "" || c.Strategy.Kind == "" {
return fmt.Errorf("invalid IM contract %q", key)
}
}
return nil
}

View File

@@ -0,0 +1,32 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package catalog
import "testing"
func TestWholeRequestPartialRecoveryContracts(t *testing.T) {
for _, key := range []ContractKey{
"im +feed-shortcut-create",
"im +feed-shortcut-remove",
"im +flag-cancel",
} {
contract, ok := Lookup(key)
if !ok {
t.Fatalf("missing contract %q", key)
}
if contract.PartialRecovery != PartialRecoveryWholeRequest {
t.Fatalf("%s partial recovery = %q", key, contract.PartialRecovery)
}
}
remove, _ := Lookup("im +feed-shortcut-remove")
if remove.ReplayMode != ReplaySafe {
t.Fatalf("feed shortcut remove replay mode = %q", remove.ReplayMode)
}
urgent, _ := Lookup("im messages urgent_app")
if urgent.PartialRecovery != PartialRecoveryFailedItemsOnly {
t.Fatalf("urgent app partial recovery = %q", urgent.PartialRecovery)
}
}

View File

@@ -0,0 +1,139 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
// Package catalog defines the static IM command completion contract catalog.
package catalog
type ContractKey string
type StrategyKind string
const (
EntityReadKind StrategyKind = "entity_read"
CollectionReadKind StrategyKind = "collection_read"
SearchReadKind StrategyKind = "search_read"
MaterializeReadKind StrategyKind = "materialize_read"
AuthoritativeAckKind StrategyKind = "authoritative_ack"
RequiredResultKind StrategyKind = "required_result"
BatchPartialKind StrategyKind = "batch_partial"
RequiredResultBatchPartialKind StrategyKind = "required_result_batch_partial"
ResponseSetAssertionKind StrategyKind = "response_set_assertion"
AcceptanceOnlyKind StrategyKind = "acceptance_only"
)
func (k StrategyKind) IsWrite() bool {
switch k {
case AuthoritativeAckKind, RequiredResultKind, BatchPartialKind,
RequiredResultBatchPartialKind, ResponseSetAssertionKind, AcceptanceOnlyKind:
return true
default:
return false
}
}
func (k StrategyKind) IsRead() bool {
switch k {
case EntityReadKind, CollectionReadKind, SearchReadKind, MaterializeReadKind:
return true
default:
return false
}
}
type ReplayMode string
const (
ReplayForbidden ReplayMode = "forbidden"
ReplaySafe ReplayMode = "safe"
ReplaySameIdempotencyKey ReplayMode = "same_idempotency_key"
)
type PartialRecoveryMode string
const (
PartialRecoveryWholeRequest PartialRecoveryMode = "whole_request"
PartialRecoveryFailedItemsOnly PartialRecoveryMode = "failed_items_only"
)
type AssertionMode string
const (
AssertRequestedPresent AssertionMode = "requested_present"
AssertRequestedAbsent AssertionMode = "requested_absent"
)
type RequiredShape uint8
const (
RequiredTopString RequiredShape = iota + 1
RequiredTopObject
RequiredNestedString
)
type EvidenceShape uint8
const (
EvidenceStrings EvidenceShape = iota + 1
EvidenceObjects
EvidenceNestedObjects
EvidenceFeedObjects
EvidenceNestedFeedObjects
EvidenceStatusObjects
)
type RequiredSpec struct {
Shape RequiredShape
Field string
Child string
}
type EvidenceSpec struct {
Shape EvidenceShape
Field string
IDField string
Container string
}
type Strategy struct {
Kind StrategyKind
Required RequiredSpec
Request EvidenceSpec
Failures []EvidenceSpec
Pending []EvidenceSpec
ResponseSets []EvidenceSpec
Assertion AssertionMode
ResultLedger *EvidenceSpec
// CollectionField is only used by the two fixed IM search strategies to
// determine whether an exhausted search returned no candidates. It is not
// a general response path or field extractor.
CollectionField string
RequiresMaterialization bool
ReadHint string
}
type HelpPolicy string
const (
HelpCompleteness HelpPolicy = "completeness"
HelpAcceptanceOnly HelpPolicy = "acceptance_only"
HintBatchReactions = "This result covers only the returned reaction fragments; use `im reactions list` to exhaust one message's reactions."
)
func (p HelpPolicy) Text() string {
switch p {
case HelpCompleteness:
return "Completeness: use --page-all --page-limit 0 for exhaustive output; only meta.complete=true proves completion."
case HelpAcceptanceOnly:
return "Verify the final state with lark-cli im chat.moderation get --chat-id <same_chat_id> --as <same_identity>."
default:
return ""
}
}
type Contract struct {
Key ContractKey
Strategy Strategy
ReplayMode ReplayMode
PartialRecovery PartialRecoveryMode
HelpPolicy HelpPolicy
}

View File

@@ -0,0 +1,43 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"strings"
"github.com/spf13/cobra"
)
const (
helpContractAnnotation = "imcontract.help.contract-key"
helpSameKeyReplay = "Idempotent retry: generate the key outside this command, then reuse the same literal with unchanged parameters on every retry."
)
func AnnotateHelpContract(cmd *cobra.Command, key ContractKey) {
if cmd == nil || key == "" {
return
}
if cmd.Annotations == nil {
cmd.Annotations = map[string]string{}
}
cmd.Annotations[helpContractAnnotation] = string(key)
}
func HelpText(cmd *cobra.Command) string {
if cmd == nil || !cmd.Runnable() || cmd.Annotations == nil {
return ""
}
contract, ok := Lookup(ContractKey(cmd.Annotations[helpContractAnnotation]))
if !ok {
return ""
}
var lines []string
if policy := contract.HelpPolicy.Text(); policy != "" {
lines = append(lines, policy)
}
if contract.ReplayMode == ReplaySameIdempotencyKey {
lines = append(lines, helpSameKeyReplay)
}
return strings.Join(lines, "\n")
}

View File

@@ -0,0 +1,88 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"testing"
"github.com/spf13/cobra"
)
func TestHelpPolicyTextUsesOnlyApprovedTemplates(t *testing.T) {
tests := []struct {
policy HelpPolicy
want string
}{
{HelpCompleteness, "Completeness: use --page-all --page-limit 0 for exhaustive output; only meta.complete=true proves completion."},
{HelpAcceptanceOnly, "Verify the final state with lark-cli im chat.moderation get --chat-id <same_chat_id> --as <same_identity>."},
{HelpPolicy("unknown"), ""},
}
for _, tt := range tests {
if got := tt.policy.Text(); got != tt.want {
t.Fatalf("HelpPolicy(%q).Text() = %q, want %q", tt.policy, got, tt.want)
}
}
}
func TestRegistryHelpPolicies(t *testing.T) {
tests := []struct {
key ContractKey
want HelpPolicy
}{
{"im +chat-list", HelpCompleteness},
{"im +messages-search", HelpCompleteness},
{"im chat.moderation get", HelpCompleteness},
{"im +messages-send", ""},
{"im messages merge_forward", ""},
{"im chat.moderation update", HelpAcceptanceOnly},
{"im +flag-create", ""},
}
for _, tt := range tests {
contract, ok := Lookup(tt.key)
if !ok {
t.Fatalf("missing contract %q", tt.key)
}
if contract.HelpPolicy != tt.want {
t.Fatalf("%s HelpPolicy = %q, want %q", tt.key, contract.HelpPolicy, tt.want)
}
}
}
func TestHelpTextIsLazyAndRunnableOnly(t *testing.T) {
cmd := &cobra.Command{Use: "+chat-list", Short: "List chats", Run: func(*cobra.Command, []string) {}}
AnnotateHelpContract(cmd, "im +chat-list")
if cmd.Long != "" || cmd.Short != "List chats" {
t.Fatalf("annotation changed visible help fields: Short=%q Long=%q", cmd.Short, cmd.Long)
}
if got := HelpText(cmd); got != HelpCompleteness.Text() {
t.Fatalf("HelpText() = %q", got)
}
parent := &cobra.Command{Use: "im"}
AnnotateHelpContract(parent, "im +chat-list")
if got := HelpText(parent); got != "" {
t.Fatalf("parent HelpText() = %q, want empty", got)
}
}
func TestHelpTextAddsSameKeyReplayOnlyToApplicableCommands(t *testing.T) {
const approvedSameKeyText = "Idempotent retry: generate the key outside this command, then reuse the same literal with unchanged parameters on every retry."
if helpSameKeyReplay != approvedSameKeyText {
t.Fatalf("same-key help = %q, want approved text %q", helpSameKeyReplay, approvedSameKeyText)
}
tests := []struct {
key ContractKey
want string
}{
{"im +messages-send", approvedSameKeyText},
{"im +chat-create", approvedSameKeyText},
{"im +chat-update", ""},
}
for _, tt := range tests {
cmd := &cobra.Command{Use: "leaf", Run: func(*cobra.Command, []string) {}}
AnnotateHelpContract(cmd, tt.key)
if got := HelpText(cmd); got != tt.want {
t.Fatalf("%s HelpText() = %q, want %q", tt.key, got, tt.want)
}
}
}

View File

@@ -0,0 +1,45 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import "github.com/larksuite/cli/errs"
// NormalizeHTTPError makes HTTP status authoritative for contract-managed IM
// responses. It prevents a JSON body with code 0 or an unknown business code
// from hiding an HTTP failure. Non-IM callers do not opt into this behavior.
func NormalizeHTTPError(status int, logID string, err error) error {
if status < 400 {
return err
}
if status >= 500 {
normalized := errs.NewNetworkError(
errs.SubtypeNetworkServer,
"HTTP %d server error",
status,
).WithCode(status).WithRetryable()
if logID != "" {
normalized.WithLogID(logID)
}
return normalized
}
if status == 429 {
normalized := errs.NewAPIError(errs.SubtypeRateLimit, "HTTP 429 rate limit").WithCode(status)
if logID != "" {
normalized.WithLogID(logID)
}
return normalized
}
if err != nil {
return err
}
subtype := errs.SubtypeUnknown
if status == 404 {
subtype = errs.SubtypeNotFound
}
normalized := errs.NewAPIError(subtype, "HTTP %d request failed", status).WithCode(status)
if logID != "" {
normalized.WithLogID(logID)
}
return normalized
}

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"testing"
"github.com/larksuite/cli/errs"
)
func TestNormalizeHTTPError(t *testing.T) {
original := errs.NewAPIError(errs.SubtypeUnknown, "business error").WithCode(123)
got := NormalizeHTTPError(503, "log-id", original)
problem, ok := errs.ProblemOf(got)
if !ok || problem.Category != errs.CategoryNetwork ||
problem.Subtype != errs.SubtypeNetworkServer ||
problem.Code != 503 || problem.LogID != "log-id" || !problem.Retryable {
t.Fatalf("normalized problem = %#v, err=%T %v", problem, got, got)
}
rateLimited := NormalizeHTTPError(429, "rate-log", nil)
rateProblem, ok := errs.ProblemOf(rateLimited)
if !ok || rateProblem.Category != errs.CategoryAPI ||
rateProblem.Subtype != errs.SubtypeRateLimit ||
rateProblem.Code != 429 || rateProblem.LogID != "rate-log" || rateProblem.Retryable {
t.Fatalf("rate-limit problem = %#v, err=%T %v", rateProblem, rateLimited, rateLimited)
}
notFound := NormalizeHTTPError(404, "", nil)
notFoundProblem, ok := errs.ProblemOf(notFound)
if !ok || notFoundProblem.Subtype != errs.SubtypeNotFound ||
notFoundProblem.Code != 404 || notFoundProblem.Retryable {
t.Fatalf("not-found problem = %#v, err=%T %v", notFoundProblem, notFound, notFound)
}
if unchanged := NormalizeHTTPError(200, "", original); unchanged != original {
t.Fatalf("successful status was normalized: %T %v", unchanged, unchanged)
}
}

View File

@@ -0,0 +1,32 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"fmt"
"maps"
)
const IdentityDefaultedNoticeKey = "identity_defaulted"
// IdentityDefaultedMessage explains both the observed choice and why callers
// should make it explicit when reproducibility matters.
func IdentityDefaultedMessage(identity string) string {
return fmt.Sprintf("--as was omitted; this IM write used %s. Pass --as explicitly for reproducible behavior.", identity)
}
// WithIdentityDefaultedNotice returns a copy of base with the command-scoped
// notice added. The copy prevents an invocation-specific fact from leaking
// into the process-wide update/skills notice map.
func WithIdentityDefaultedNotice(base map[string]interface{}, identity string) map[string]interface{} {
notice := maps.Clone(base)
if notice == nil {
notice = make(map[string]interface{}, 1)
}
notice[IdentityDefaultedNoticeKey] = map[string]interface{}{
"resolved": identity,
"message": IdentityDefaultedMessage(identity),
}
return notice
}

View File

@@ -0,0 +1,31 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import "testing"
func TestWithIdentityDefaultedNoticeMergesWithoutMutatingBase(t *testing.T) {
base := map[string]interface{}{
"update": map[string]interface{}{"available": true},
}
got := WithIdentityDefaultedNotice(base, "bot")
if _, ok := base[IdentityDefaultedNoticeKey]; ok {
t.Fatalf("base notice was mutated: %#v", base)
}
if got["update"] == nil {
t.Fatalf("existing notice was lost: %#v", got)
}
identity, ok := got[IdentityDefaultedNoticeKey].(map[string]interface{})
if !ok {
t.Fatalf("identity notice = %#v", got[IdentityDefaultedNoticeKey])
}
if identity["resolved"] != "bot" {
t.Fatalf("resolved = %#v, want bot", identity["resolved"])
}
if identity["message"] != IdentityDefaultedMessage("bot") {
t.Fatalf("message = %#v", identity["message"])
}
}

View File

@@ -0,0 +1,248 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"encoding/json"
"fmt"
"strings"
)
type Completion struct {
Status string `json:"status"`
RequestedCount int `json:"requested_count"`
SucceededCount int `json:"succeeded_count"`
FailedCount int `json:"failed_count"`
PendingCount int `json:"pending_count"`
SucceededItems []any `json:"succeeded_items"`
FailedItems []any `json:"failed_items"`
PendingItems []any `json:"pending_items"`
RetryScope string `json:"retry_scope"`
}
type ledgerItem struct {
key string
value any
}
type extraction struct {
items []ledgerItem
rawCount int
selectedCount int
rejectedCount int
present bool
}
func extract(root map[string]any, spec evidenceSpec) extraction {
if root == nil || spec.Field == "" {
return extraction{}
}
raw, present := root[spec.Field]
if !present {
return extraction{}
}
values, ok := raw.([]any)
out := extraction{present: true}
if !ok {
out.rejectedCount = 1
return out
}
out.rawCount = len(values)
for _, value := range values {
item, ok := extractItem(value, spec)
if !ok {
out.rejectedCount++
continue
}
out.selectedCount++
out.items = append(out.items, item)
}
out.items = uniqueItems(out.items)
return out
}
func extractItem(value any, spec evidenceSpec) (ledgerItem, bool) {
switch spec.Shape {
case evidenceStrings:
return stringItem(value)
case evidenceObjects:
object, ok := value.(map[string]any)
if !ok {
return ledgerItem{}, false
}
return stringItem(object[spec.IDField])
case evidenceNestedObjects:
object, ok := nestedObject(value, spec.Container)
if !ok {
return ledgerItem{}, false
}
return stringItem(object[spec.IDField])
case evidenceFeedObjects:
object, ok := value.(map[string]any)
if !ok {
return ledgerItem{}, false
}
return feedItem(object)
case evidenceNestedFeedObjects:
object, ok := nestedObject(value, spec.Container)
if !ok {
return ledgerItem{}, false
}
return feedItem(object)
case evidenceStatusObjects:
object, ok := value.(map[string]any)
if !ok {
return ledgerItem{}, false
}
status := nonEmptyString(object["status"])
if status != "ok" && status != "failed" {
return ledgerItem{}, false
}
return stringItem(object[spec.IDField])
default:
return ledgerItem{}, false
}
}
func nestedObject(value any, field string) (map[string]any, bool) {
object, ok := value.(map[string]any)
if !ok {
return nil, false
}
nested, ok := object[field].(map[string]any)
return nested, ok
}
func stringItem(value any) (ledgerItem, bool) {
id := stableID(value)
if id == "" {
return ledgerItem{}, false
}
return ledgerItem{key: id, value: id}, true
}
func feedItem(object map[string]any) (ledgerItem, bool) {
feedID := stableID(object["feed_id"])
feedType := stableID(object["feed_type"])
if feedID == "" || feedType == "" {
return ledgerItem{}, false
}
return ledgerItem{
key: feedType + "\x00" + feedID,
value: map[string]any{
"feed_id": feedID, "feed_type": feedType,
},
}, true
}
func nonEmptyString(value any) string {
text, ok := value.(string)
if !ok {
return ""
}
return strings.TrimSpace(text)
}
func stableID(value any) string {
switch id := value.(type) {
case string:
return strings.TrimSpace(id)
case json.Number:
return string(id)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprint(id)
default:
return ""
}
}
func uniqueItems(items []ledgerItem) []ledgerItem {
out := make([]ledgerItem, 0, len(items))
seen := make(map[string]struct{}, len(items))
for _, item := range items {
if item.key == "" {
continue
}
if _, ok := seen[item.key]; ok {
continue
}
seen[item.key] = struct{}{}
out = append(out, item)
}
return out
}
func completion(requested, failed, pending []ledgerItem, recovery PartialRecoveryMode) Completion {
requested = uniqueItems(requested)
requestedSet := make(map[string]struct{}, len(requested))
for _, item := range requested {
requestedSet[item.key] = struct{}{}
}
filterRequested := func(items []ledgerItem, excluded map[string]struct{}) []ledgerItem {
out := make([]ledgerItem, 0, len(items))
for _, item := range uniqueItems(items) {
if _, ok := requestedSet[item.key]; !ok {
continue
}
if _, blocked := excluded[item.key]; blocked {
continue
}
out = append(out, item)
}
return out
}
// A contradictory pending+failed response is treated as pending. Pending
// means the final state is unknown, so authorizing a retry would be unsafe.
pending = filterRequested(pending, nil)
pendingSet := make(map[string]struct{}, len(pending))
for _, item := range pending {
pendingSet[item.key] = struct{}{}
}
failed = filterRequested(failed, pendingSet)
blocked := make(map[string]struct{}, len(failed)+len(pending))
for key := range pendingSet {
blocked[key] = struct{}{}
}
for _, item := range failed {
blocked[item.key] = struct{}{}
}
succeeded := make([]ledgerItem, 0, len(requested))
for _, item := range requested {
if _, exists := blocked[item.key]; !exists {
succeeded = append(succeeded, item)
}
}
status := "complete"
retryScope := "none"
if len(failed) > 0 || len(pending) > 0 {
status = "partial"
switch {
case len(pending) > 0:
retryScope = "none"
case recovery == PartialRecoveryWholeRequest:
retryScope = "whole_request"
default:
retryScope = "failed_items_only"
}
}
values := func(items []ledgerItem) []any {
out := make([]any, 0, len(items))
for _, item := range items {
out = append(out, item.value)
}
return out
}
return Completion{
Status: status,
RequestedCount: len(requested),
SucceededCount: len(succeeded),
FailedCount: len(failed),
PendingCount: len(pending),
SucceededItems: values(succeeded),
FailedItems: values(failed),
PendingItems: values(pending),
RetryScope: retryScope,
}
}

View File

@@ -0,0 +1,43 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
// MaterializationStatus records the IM-only search-to-detail reconciliation.
// RequestedIDs and ResolvedIDs are internal evidence and are never serialized;
// only missing requested IDs may be exposed for targeted recovery.
type MaterializationStatus struct {
RequestedIDs []string `json:"-"`
ResolvedIDs []string `json:"-"`
MissingMessageIDs []string
UnresolvedHitCount int
UnexpectedMessageCount int
Cause error `json:"-"`
}
func (s MaterializationStatus) complete() bool {
return s.Cause == nil &&
len(s.MissingMessageIDs) == 0 &&
s.UnresolvedHitCount == 0 &&
s.UnexpectedMessageCount == 0 &&
len(s.RequestedIDs) == len(s.ResolvedIDs)
}
func (s MaterializationStatus) ledger() map[string]any {
status := "partial"
if s.complete() {
status = "complete"
}
missing := append([]string(nil), s.MissingMessageIDs...)
if missing == nil {
missing = []string{}
}
return map[string]any{
"status": status,
"requested_count": len(s.RequestedIDs),
"resolved_count": len(s.ResolvedIDs),
"missing_message_ids": missing,
"unresolved_hit_count": s.UnresolvedHitCount,
"unexpected_message_count": s.UnexpectedMessageCount,
}
}

View File

@@ -0,0 +1,215 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import "github.com/larksuite/cli/internal/output"
type MessageMentionRequest struct {
IDs []string
All bool
}
type MessageMentionConfirmation struct {
RequestedID string `json:"requested_id"`
ID string `json:"id"`
IDType string `json:"id_type"`
Key string `json:"key"`
}
type MessageMentionResult struct {
Status string `json:"status"`
Requested []string `json:"requested"`
Confirmed []MessageMentionConfirmation `json:"confirmed"`
Missing []string `json:"missing"`
UnattributedRequested []string `json:"unattributed_requested,omitempty"`
All string `json:"all"`
RetryScope string `json:"retry_scope"`
}
// BuildMessageMentionResult compares the structured mention request with the
// returned mention entries. Exact open_id matches are confirmed; unmatched or
// ambiguous entries remain unattributed and never authorize replay.
func BuildMessageMentionResult(request MessageMentionRequest, response any) MessageMentionResult {
requested := append([]string(nil), request.IDs...)
result := MessageMentionResult{
Requested: requested,
Confirmed: []MessageMentionConfirmation{},
Missing: []string{},
All: "not_requested",
RetryScope: "none",
}
if request.All {
result.All = "accepted_unverified"
if len(requested) == 0 {
result.Status = "accepted_unverified"
return result
}
}
mentions, ambiguous := parseResponseMentions(response)
confirmed := make([]MessageMentionConfirmation, 0, len(requested))
confirmedIDs := make(map[string]struct{}, len(requested))
responseKeys := make(map[string]struct{}, len(mentions))
unknownEvidence := false
for _, mention := range mentions {
if mention.id == "all" || mention.id == "@_all" {
if !request.All {
unknownEvidence = true
}
continue
}
if mention.idType != "open_id" {
unknownEvidence = true
continue
}
if !contains(requested, mention.id) {
unknownEvidence = true
continue
}
if _, duplicate := responseKeys[mention.key]; duplicate {
ambiguous = true
continue
}
responseKeys[mention.key] = struct{}{}
if _, duplicate := confirmedIDs[mention.id]; duplicate {
ambiguous = true
continue
}
confirmedIDs[mention.id] = struct{}{}
confirmed = append(confirmed, MessageMentionConfirmation{
RequestedID: mention.id,
ID: mention.id,
IDType: mention.idType,
Key: mention.key,
})
}
unresolved := make([]string, 0, len(requested))
for _, id := range requested {
if _, ok := confirmedIDs[id]; !ok {
unresolved = append(unresolved, id)
}
}
if ambiguous || unknownEvidence || len(unresolved) > 0 {
result.Status = "partial_unattributed"
result.Confirmed = confirmed
if len(unresolved) > 0 {
result.UnattributedRequested = unresolved
} else {
// Do not place the same IDs in both confirmed and unattributed
// sets when extra entries make the result ambiguous.
result.Confirmed = []MessageMentionConfirmation{}
result.UnattributedRequested = append([]string(nil), requested...)
}
return result
}
result.Confirmed = confirmed
if request.All {
result.Status = "accepted_unverified"
} else {
result.Status = "complete"
}
return result
}
type responseMention struct {
key string
id string
idType string
}
func parseResponseMentions(response any) ([]responseMention, bool) {
if response == nil {
return nil, false
}
values, ok := response.([]any)
if !ok {
return nil, true
}
mentions := make([]responseMention, 0, len(values))
for _, value := range values {
object, ok := value.(map[string]any)
if !ok {
return mentions, true
}
mention := responseMention{
key: nonEmptyString(object["key"]),
id: nonEmptyString(object["id"]),
idType: nonEmptyString(object["id_type"]),
}
if mention.id == "all" || mention.id == "@_all" {
mentions = append(mentions, mention)
continue
}
if mention.key == "" || mention.id == "" || mention.idType == "" {
return mentions, true
}
mentions = append(mentions, mention)
}
return mentions, false
}
func contains(values []string, target string) bool {
for _, value := range values {
if value == target {
return true
}
}
return false
}
func finalizeMessageMentions(data any) (Result, error) {
root, err := checkedResponse(data)
if err != nil {
return Result{}, err
}
raw, present := root["mention_result"]
if !present {
return Result{OK: true, Data: root}, nil
}
mention, ok := raw.(MessageMentionResult)
if !ok || !validMentionResultShape(mention) {
return Result{}, invalidEvidence("mention_result")
}
result := Result{OK: true, Data: root}
switch mention.Status {
case "complete", "accepted_unverified":
return result, nil
case "partial", "partial_unattributed":
result.OK = false
result.ExitCode = output.ExitAPI
return result, nil
default:
return Result{}, invalidEvidence("mention_result")
}
}
func validMentionResultShape(result MessageMentionResult) bool {
if result.RetryScope != "none" {
return false
}
for _, confirmation := range result.Confirmed {
if confirmation.RequestedID == "" || confirmation.ID == "" ||
confirmation.IDType == "" || confirmation.Key == "" {
return false
}
}
if result.All != "not_requested" && result.All != "accepted_unverified" {
return false
}
switch result.Status {
case "complete":
return len(result.Missing) == 0 && result.All == "not_requested"
case "accepted_unverified":
return len(result.Missing) == 0 && result.All == "accepted_unverified"
case "partial":
return len(result.Requested) > 0 && len(result.Missing) > 0
case "partial_unattributed":
return len(result.Requested) > 0 && len(result.Missing) == 0 &&
len(result.UnattributedRequested) > 0
default:
return false
}
}

View File

@@ -0,0 +1,214 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"testing"
"github.com/larksuite/cli/internal/output"
)
func TestBuildMessageMentionResult(t *testing.T) {
tests := []struct {
name string
request MessageMentionRequest
response any
wantStatus string
wantConfirmed int
wantMissing []string
wantUnattrib []string
wantAll string
}{
{
name: "all accepted without notification proof",
request: MessageMentionRequest{All: true},
wantStatus: "accepted_unverified",
wantAll: "accepted_unverified",
},
{
name: "all ignores unverified response shape",
request: MessageMentionRequest{All: true},
response: []any{
map[string]any{"key": "@_all", "id": "all"},
},
wantStatus: "accepted_unverified",
wantAll: "accepted_unverified",
},
{
name: "open ids confirmed exactly",
request: MessageMentionRequest{IDs: []string{"ou_alpha", "ou_beta"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_alpha", "id_type": "open_id"},
map[string]any{"key": "@_user_2", "id": "ou_beta", "id_type": "open_id"},
},
wantStatus: "complete",
wantConfirmed: 2,
wantAll: "not_requested",
},
{
name: "missing open id stays unattributed",
request: MessageMentionRequest{IDs: []string{"ou_alpha", "ou_beta"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_alpha", "id_type": "open_id"},
},
wantStatus: "partial_unattributed",
wantConfirmed: 1,
wantUnattrib: []string{"ou_beta"},
wantAll: "not_requested",
},
{
name: "normalized user id cannot be guessed",
request: MessageMentionRequest{IDs: []string{"u_alpha"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_normalized", "id_type": "open_id"},
},
wantStatus: "partial_unattributed",
wantUnattrib: []string{"u_alpha"},
wantAll: "not_requested",
},
{
name: "unknown response evidence is unattributed",
request: MessageMentionRequest{IDs: []string{"ou_alpha"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_unknown", "id_type": "open_id"},
},
wantStatus: "partial_unattributed",
wantUnattrib: []string{"ou_alpha"},
wantAll: "not_requested",
},
{
name: "duplicate response key is unattributed",
request: MessageMentionRequest{IDs: []string{"ou_alpha", "ou_beta"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_alpha", "id_type": "open_id"},
map[string]any{"key": "@_user_1", "id": "ou_beta", "id_type": "open_id"},
},
wantStatus: "partial_unattributed",
wantConfirmed: 1,
wantUnattrib: []string{"ou_beta"},
wantAll: "not_requested",
},
{
name: "extra unknown evidence invalidates otherwise complete mapping",
request: MessageMentionRequest{IDs: []string{"ou_alpha"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_alpha", "id_type": "open_id"},
map[string]any{"key": "@_user_2", "id": "ou_unknown", "id_type": "open_id"},
},
wantStatus: "partial_unattributed",
wantUnattrib: []string{"ou_alpha"},
wantAll: "not_requested",
},
{
name: "duplicate requested evidence invalidates otherwise complete mapping",
request: MessageMentionRequest{IDs: []string{"ou_alpha"}},
response: []any{
map[string]any{"key": "@_user_1", "id": "ou_alpha", "id_type": "open_id"},
map[string]any{"key": "@_user_2", "id": "ou_alpha", "id_type": "open_id"},
},
wantStatus: "partial_unattributed",
wantUnattrib: []string{"ou_alpha"},
wantAll: "not_requested",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := BuildMessageMentionResult(tt.request, tt.response)
if got.Status != tt.wantStatus {
t.Fatalf("status = %v, want %q", got.Status, tt.wantStatus)
}
if got.RetryScope != "none" {
t.Fatalf("retry_scope = %v, want none", got.RetryScope)
}
if got.All != tt.wantAll {
t.Fatalf("all = %v, want %q", got.All, tt.wantAll)
}
if len(got.Confirmed) != tt.wantConfirmed {
t.Fatalf("confirmed = %#v, want len %d", got.Confirmed, tt.wantConfirmed)
}
assertStringSlice(t, got.Missing, tt.wantMissing)
assertStringSlice(t, got.UnattributedRequested, tt.wantUnattrib)
})
}
}
func TestFinalizeMessageMentionResult(t *testing.T) {
contract, ok := Lookup("im +messages-send")
if !ok {
t.Fatal("messages-send contract missing")
}
tests := []struct {
name string
mention any
wantOK bool
wantExit int
wantErr bool
}{
{name: "absent stays compatible", wantOK: true},
{name: "complete", mention: validMentionResult("complete"), wantOK: true},
{name: "accepted all", mention: validMentionResult("accepted_unverified"), wantOK: true},
{name: "partial", mention: validMentionResult("partial"), wantExit: output.ExitAPI},
{name: "partial unattributed", mention: validMentionResult("partial_unattributed"), wantExit: output.ExitAPI},
{name: "unknown status", mention: validMentionResult("mystery"), wantErr: true},
{name: "replay scope cannot authorize replay", mention: MessageMentionResult{
Status: "partial", Requested: []string{"ou_a"}, Confirmed: []MessageMentionConfirmation{},
Missing: []string{"ou_a"}, All: "not_requested", RetryScope: "whole_request",
}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := map[string]any{"message_id": "om_result"}
if tt.mention != nil {
data["mention_result"] = tt.mention
}
got, err := NewSession(contract).FinalizeSuccess(data)
if (err != nil) != tt.wantErr {
t.Fatalf("FinalizeSuccess() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
if got.OK != tt.wantOK || got.ExitCode != tt.wantExit {
t.Fatalf("result = %#v, want ok=%v exit=%d", got, tt.wantOK, tt.wantExit)
}
})
}
}
func validMentionResult(status string) MessageMentionResult {
result := MessageMentionResult{
Status: status,
Requested: []string{},
Confirmed: []MessageMentionConfirmation{},
Missing: []string{},
All: "not_requested",
RetryScope: "none",
}
switch status {
case "accepted_unverified":
result.All = "accepted_unverified"
case "partial":
result.Requested = []string{"ou_a"}
result.Missing = []string{"ou_a"}
case "partial_unattributed":
result.Requested = []string{"u_a"}
result.UnattributedRequested = []string{"u_a"}
}
return result
}
func assertStringSlice(t *testing.T, got, want []string) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("value = %#v, want %#v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("value = %#v, want %#v", got, want)
}
}
}

View File

@@ -0,0 +1,118 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/output"
)
// BuildJQOutputFallback returns the self-contained result emitted when jq
// presentation fails after an IM write has already been finalized.
func BuildJQOutputFallback(result Result) (output.Envelope, error) {
problem := errs.NewAPIError(
errs.SubtypeUnknown,
"Output failed after the IM write completed",
)
return buildOutputFallback(result, &problem.Problem), output.PartialFailure(output.ExitAPI)
}
// BuildContentSafetyOutputFallback returns the self-contained result emitted
// when content-safety blocks presentation after an IM write has already been
// finalized.
func BuildContentSafetyOutputFallback(result Result) (output.Envelope, error) {
problem := errs.NewContentSafetyError(
errs.SubtypeContentSafety,
"Output blocked after the IM write completed",
)
return buildOutputFallback(result, &problem.Problem), output.PartialFailure(output.ExitContentSafety)
}
func buildOutputFallback(result Result, problem *errs.Problem) output.Envelope {
return output.Envelope{
OK: false,
Data: map[string]any{
"completion": allowlistedCompletion(result.Data),
},
Error: problem,
}
}
func allowlistedCompletion(data any) map[string]any {
summary := map[string]any{
"status": "complete",
"retry_scope": "none",
}
root, ok := data.(map[string]any)
if !ok {
return summary
}
completionValue, hasCompletion := root["completion"]
switch completion := completionValue.(type) {
case Completion:
copyCompletionStatus(summary, completion.Status)
summary["requested_count"] = completion.RequestedCount
summary["succeeded_count"] = completion.SucceededCount
summary["failed_count"] = completion.FailedCount
summary["pending_count"] = completion.PendingCount
copyCompletionRetryScope(summary, completion.RetryScope)
return summary
case map[string]any:
if value, ok := completion["status"].(string); ok {
copyCompletionStatus(summary, value)
}
copyCompletionCount(summary, completion, "requested_count")
copyCompletionCount(summary, completion, "succeeded_count")
copyCompletionCount(summary, completion, "failed_count")
copyCompletionCount(summary, completion, "pending_count")
if value, exists := completion["final_state_verified"]; exists {
if verified, valid := value.(bool); valid {
summary["final_state_verified"] = verified
}
}
if value, ok := completion["retry_scope"].(string); ok {
copyCompletionRetryScope(summary, value)
}
}
if !hasCompletion {
if mention, ok := root["mention_result"].(MessageMentionResult); ok {
copyCompletionStatus(summary, mention.Status)
copyCompletionRetryScope(summary, mention.RetryScope)
}
}
return summary
}
func copyCompletionStatus(dst map[string]any, value string) {
switch value {
case "complete", "partial", "accepted_unverified", "partial_unattributed":
dst["status"] = value
}
}
func copyCompletionRetryScope(dst map[string]any, value string) {
switch value {
case "none", "whole_request", "failed_items_only":
dst["retry_scope"] = value
}
}
func copyCompletionCount(dst, src map[string]any, key string) {
switch value := src[key].(type) {
case int:
dst[key] = value
case int32:
dst[key] = value
case int64:
dst[key] = value
case uint:
dst[key] = value
case uint32:
dst[key] = value
case uint64:
dst[key] = value
case float64:
dst[key] = value
}
}

View File

@@ -0,0 +1,140 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"encoding/json"
"strings"
"testing"
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/output"
)
func TestOutputFallbackBuildsCompletionByAllowlist(t *testing.T) {
const secret = "SECRET_MARKER"
tests := []struct {
name string
result Result
wantStatus string
wantScope string
wantCounts bool
wantFinal bool
}{
{
name: "completed required result",
result: Result{OK: true, Data: map[string]any{
"message_id": secret,
}},
wantStatus: "complete",
wantScope: "none",
},
{
name: "batch partial",
result: Result{Data: map[string]any{
"completion": Completion{
Status: "partial",
RequestedCount: 2,
SucceededCount: 1,
FailedCount: 1,
FailedItems: []any{secret},
RetryScope: "failed_items_only",
},
}},
wantStatus: "partial",
wantScope: "failed_items_only",
wantCounts: true,
},
{
name: "accepted unverified",
result: Result{OK: true, Data: map[string]any{
"completion": map[string]any{
"status": "accepted_unverified",
"final_state_verified": false,
"retry_scope": "none",
"message": secret,
},
}},
wantStatus: "accepted_unverified",
wantScope: "none",
wantFinal: true,
},
{
name: "mention partial",
result: Result{Data: map[string]any{
"mention_result": MessageMentionResult{
Status: "partial_unattributed",
Requested: []string{secret},
Confirmed: []MessageMentionConfirmation{},
Missing: []string{},
UnattributedRequested: []string{secret},
All: "not_requested",
RetryScope: "none",
},
}},
wantStatus: "partial_unattributed",
wantScope: "none",
},
{
name: "unknown recovery values are not trusted",
result: Result{OK: true, Data: map[string]any{
"completion": map[string]any{
"status": secret,
"retry_scope": secret,
},
}},
wantStatus: "complete",
wantScope: "none",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
env, signal := BuildJQOutputFallback(tc.result)
if output.ExitCodeOf(signal) != output.ExitAPI {
t.Fatalf("exit = %d", output.ExitCodeOf(signal))
}
raw, err := json.Marshal(env)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(raw), secret) {
t.Fatalf("fallback leaked payload: %s", raw)
}
data := env.Data.(map[string]any)
if len(data) != 1 {
t.Fatalf("data = %#v", data)
}
completion := data["completion"].(map[string]any)
if completion["status"] != tc.wantStatus || completion["retry_scope"] != tc.wantScope {
t.Fatalf("completion = %#v", completion)
}
_, hasCounts := completion["requested_count"]
if hasCounts != tc.wantCounts {
t.Fatalf("completion counts presence = %v, want %v: %#v", hasCounts, tc.wantCounts, completion)
}
_, hasFinal := completion["final_state_verified"]
if hasFinal != tc.wantFinal {
t.Fatalf("final state presence = %v, want %v: %#v", hasFinal, tc.wantFinal, completion)
}
for _, forbidden := range []string{"succeeded_items", "failed_items", "pending_items", "message"} {
if _, exists := completion[forbidden]; exists {
t.Fatalf("completion copied %s: %#v", forbidden, completion)
}
}
})
}
}
func TestContentSafetyOutputFallbackUsesFixedPublicProblem(t *testing.T) {
env, signal := BuildContentSafetyOutputFallback(Result{Data: map[string]any{}})
if output.ExitCodeOf(signal) != output.ExitContentSafety {
t.Fatalf("exit = %d", output.ExitCodeOf(signal))
}
problem := env.Error.(*errs.Problem)
if problem.Category != errs.CategoryPolicy || problem.Subtype != errs.SubtypeContentSafety ||
problem.Message != "Output blocked after the IM write completed" {
t.Fatalf("problem = %#v", problem)
}
}

286
internal/imcontract/read.go Normal file
View File

@@ -0,0 +1,286 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/client"
"github.com/larksuite/cli/internal/output"
)
const (
hintSinglePage = "Result is incomplete. Re-run with --page-all --page-limit 0 when exhaustive output is required."
hintPageLimit = "Result is incomplete because --page-limit was reached. Use --page-limit 0 only when exhaustive output is required."
hintReadFailed = "The read is incomplete. Retry the read; do not infer that missing items do not exist."
hintTokenUnusable = "The server did not provide a usable next page token. Report the result as incomplete."
hintStartPage = "This read started from a supplied page token and does not prove the collection was exhausted from the beginning."
hintServerTruncate = "The server truncated the result. Narrow the query range before retrying."
hintSearchEmpty = "The search was exhausted, but an empty search result does not prove that the resource does not exist."
)
type ReadOptions struct {
FullRead bool
}
// ReadResult is the IM-only interpretation of neutral pagination facts.
// Error is deliberately a copied Problem rather than the original error so
// causes and typed-error extension fields cannot leak into stdout.
type ReadResult struct {
OK bool
Data any
Meta *output.Meta
Error *errs.Problem
Hint string
ExitCode int
Cause error `json:"-"`
}
// ReadSession is independent from the write Session. It records typed
// pagination and, for explicitly opted-in searches, materialization evidence;
// it never observes raw request or response bodies.
type ReadSession struct {
contract Contract
options ReadOptions
status client.PaginationStatus
observed bool
materialization MaterializationStatus
materializationObserved bool
}
func NewReadSession(contract Contract, options ReadOptions) (*ReadSession, error) {
if !contract.Strategy.Kind.IsRead() {
return nil, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"unsupported IM read contract strategy %q",
contract.Strategy.Kind,
)
}
return &ReadSession{contract: contract, options: options}, nil
}
func (s *ReadSession) ObservePagination(status client.PaginationStatus) {
s.status = status
s.observed = true
}
func (s *ReadSession) ObserveMaterialization(status MaterializationStatus) {
s.materialization = status
s.materializationObserved = true
}
func (s *ReadSession) RequiresPagination() bool {
return s.contract.Strategy.Kind == CollectionReadKind || s.contract.Strategy.Kind == SearchReadKind
}
// FinalizeError applies the IM read retry contract to a typed error. Reads may
// be retried after transport failures and server errors. Rate limits and all
// other API or validation failures do not authorize an Agent retry.
func (s *ReadSession) FinalizeError(err error) error {
problem, ok := errs.ProblemOf(err)
if !ok {
return err
}
normalizeReadProblem(problem)
return err
}
func (s *ReadSession) Finalize(data any) (ReadResult, error) {
switch s.contract.Strategy.Kind {
case EntityReadKind, MaterializeReadKind:
return ReadResult{
OK: true,
Data: data,
Hint: s.contract.Strategy.ReadHint,
}, nil
case CollectionReadKind, SearchReadKind:
if !s.observed {
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"IM collection read completed without pagination status",
)
}
default:
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"unsupported IM read contract strategy %q",
s.contract.Strategy.Kind,
)
}
result, err := finalizePagedRead(data, s.status, s.options.FullRead)
if err != nil {
return ReadResult{}, err
}
if s.contract.Strategy.RequiresMaterialization {
result, err = s.finalizeMaterialization(result)
if err != nil {
return ReadResult{}, err
}
}
if s.contract.Strategy.Kind == SearchReadKind &&
s.status.StopReason == client.StopReasonExhausted &&
searchCollectionEmpty(data, s.contract.Strategy.CollectionField) {
result.Hint = joinHints(result.Hint, hintSearchEmpty)
}
return result, nil
}
func (s *ReadSession) finalizeMaterialization(result ReadResult) (ReadResult, error) {
if !s.materializationObserved {
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"IM search completed without materialization status",
)
}
data, ok := result.Data.(map[string]any)
if !ok {
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"IM search materialization requires an object result",
)
}
data["materialization"] = s.materialization.ledger()
result.Data = data
materializationComplete := s.materialization.complete()
if result.Meta == nil || result.Meta.Complete == nil {
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"IM search materialization requires pagination completeness",
)
}
*result.Meta.Complete = *result.Meta.Complete && materializationComplete
if materializationComplete {
if *result.Meta.Complete {
result.Hint = "Results are ready to use. Use message_id/file_key directly; do not call messages-mget."
}
return result, nil
}
result.OK = false
if result.ExitCode == 0 {
result.ExitCode = output.ExitAPI
}
materializationHint := ""
if len(s.materialization.MissingMessageIDs) > 0 {
materializationHint = "The search is incomplete. Query only materialization.missing_message_ids with im +messages-mget."
} else {
materializationHint = "The search is incomplete and cannot be safely recovered by message ID. Narrow the query before retrying."
}
result.Hint = joinHints(result.Hint, materializationHint)
if result.Error == nil && s.materialization.Cause != nil {
if problem, ok := errs.ProblemOf(s.materialization.Cause); ok {
copied := *problem
normalizeReadProblem(&copied)
result.Error = &copied
result.Cause = s.materialization.Cause
result.ExitCode = output.ExitCodeOf(s.materialization.Cause)
}
}
return result, nil
}
func finalizePagedRead(data any, status client.PaginationStatus, fullRead bool) (ReadResult, error) {
complete := false
result := ReadResult{
OK: true,
Data: data,
Meta: &output.Meta{
Complete: &complete,
PagesFetched: status.PagesFetched,
StopReason: string(status.StopReason),
NextPageToken: status.NextPageToken,
},
}
switch status.StopReason {
case client.StopReasonExhausted:
complete = true
case client.StopReasonSinglePage:
result.Hint = hintSinglePage
case client.StopReasonPageLimit:
result.Hint = hintPageLimit
case client.StopReasonStartPageToken:
result.Hint = hintStartPage
case client.StopReasonServerTruncation:
result.Hint = hintServerTruncate
if fullRead {
result.OK = false
result.ExitCode = output.ExitAPI
}
case client.StopReasonTransportError, client.StopReasonAPIError,
client.StopReasonMissingToken, client.StopReasonRepeatedToken:
if status.Cause == nil {
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"pagination stopped with %q but no typed cause was recorded",
status.StopReason,
)
}
problem, ok := errs.ProblemOf(status.Cause)
if !ok {
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"pagination stopped with an untyped cause",
)
}
copied := *problem
normalizeReadProblem(&copied)
result.OK = false
result.Error = &copied
result.ExitCode = output.ExitCodeOf(status.Cause)
result.Cause = status.Cause
switch status.StopReason {
case client.StopReasonMissingToken, client.StopReasonRepeatedToken:
result.Hint = hintTokenUnusable
default:
result.Hint = hintReadFailed
}
default:
return ReadResult{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"unsupported pagination stop reason %q",
status.StopReason,
)
}
*result.Meta.Complete = complete
return result, nil
}
func normalizeReadProblem(problem *errs.Problem) {
if problem == nil {
return
}
problem.Retryable = problem.Category == errs.CategoryNetwork ||
(problem.Category == errs.CategoryAPI && problem.Subtype == errs.SubtypeServerError)
}
func searchCollectionEmpty(data any, field string) bool {
m, ok := data.(map[string]any)
if !ok {
return false
}
value, exists := m[field]
if !exists {
return false
}
switch items := value.(type) {
case []any:
return len(items) == 0
case []map[string]any:
return len(items) == 0
default:
return false
}
}
func joinHints(first, second string) string {
if first == "" {
return second
}
if second == "" {
return first
}
return first + " " + second
}

View File

@@ -0,0 +1,401 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"encoding/json"
"strings"
"testing"
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/client"
"github.com/larksuite/cli/internal/output"
)
func TestReadCompletenessMatrix(t *testing.T) {
apiErr := errs.NewAPIError(errs.SubtypeServerError, "later page failed")
networkErr := errs.NewNetworkError(errs.SubtypeNetworkTransport, "request failed").WithRetryable()
invalidErr := errs.NewInternalError(errs.SubtypeInvalidResponse, "bad pagination")
tests := []struct {
name string
fullRead bool
status client.PaginationStatus
wantOK bool
wantDone bool
wantExit int
wantReason client.StopReason
wantError bool
wantHint string
}{
{"single exhausted", false, client.PaginationStatus{PagesFetched: 1, StopReason: client.StopReasonExhausted}, true, true, 0, client.StopReasonExhausted, false, ""},
{"single has more", false, client.PaginationStatus{PagesFetched: 1, HasMore: true, NextPageToken: "next", StopReason: client.StopReasonSinglePage}, true, false, 0, client.StopReasonSinglePage, false, "Result is incomplete. Re-run with --page-all --page-limit 0 when exhaustive output is required."},
{"all exhausted", true, client.PaginationStatus{PagesFetched: 2, StopReason: client.StopReasonExhausted}, true, true, 0, client.StopReasonExhausted, false, ""},
{"page limit", true, client.PaginationStatus{PagesFetched: 2, HasMore: true, NextPageToken: "next", StopReason: client.StopReasonPageLimit}, true, false, 0, client.StopReasonPageLimit, false, "Result is incomplete because --page-limit was reached. Use --page-limit 0 only when exhaustive output is required."},
{"start token", false, client.PaginationStatus{PagesFetched: 1, StopReason: client.StopReasonStartPageToken}, true, false, 0, client.StopReasonStartPageToken, false, hintStartPage},
{"api error", true, client.PaginationStatus{PagesFetched: 1, HasMore: true, NextPageToken: "next", StopReason: client.StopReasonAPIError, Cause: apiErr}, false, false, output.ExitAPI, client.StopReasonAPIError, true, "The read is incomplete. Retry the read; do not infer that missing items do not exist."},
{"transport error", true, client.PaginationStatus{PagesFetched: 1, HasMore: true, NextPageToken: "next", StopReason: client.StopReasonTransportError, Cause: networkErr}, false, false, output.ExitNetwork, client.StopReasonTransportError, true, "The read is incomplete. Retry the read; do not infer that missing items do not exist."},
{"missing token", true, client.PaginationStatus{PagesFetched: 1, HasMore: true, StopReason: client.StopReasonMissingToken, Cause: invalidErr}, false, false, output.ExitInternal, client.StopReasonMissingToken, true, "The server did not provide a usable next page token. Report the result as incomplete."},
{"repeated token", true, client.PaginationStatus{PagesFetched: 2, HasMore: true, StopReason: client.StopReasonRepeatedToken, Cause: invalidErr}, false, false, output.ExitInternal, client.StopReasonRepeatedToken, true, "The server did not provide a usable next page token. Report the result as incomplete."},
{"single truncation", false, client.PaginationStatus{PagesFetched: 1, StopReason: client.StopReasonServerTruncation}, true, false, 0, client.StopReasonServerTruncation, false, "The server truncated the result. Narrow the query range before retrying."},
{"full truncation", true, client.PaginationStatus{PagesFetched: 1, StopReason: client.StopReasonServerTruncation}, false, false, output.ExitAPI, client.StopReasonServerTruncation, false, "The server truncated the result. Narrow the query range before retrying."},
}
contract := mustReadContract(t, "im +chat-list")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
session, err := NewReadSession(contract, ReadOptions{FullRead: tt.fullRead})
if err != nil {
t.Fatal(err)
}
session.ObservePagination(tt.status)
got, err := session.Finalize(map[string]any{"items": []any{"a"}})
if err != nil {
t.Fatal(err)
}
if got.OK != tt.wantOK || got.ExitCode != tt.wantExit {
t.Fatalf("result OK/exit = %v/%d, want %v/%d", got.OK, got.ExitCode, tt.wantOK, tt.wantExit)
}
if got.Meta == nil || got.Meta.Complete == nil || *got.Meta.Complete != tt.wantDone {
t.Fatalf("complete = %#v, want %v", got.Meta, tt.wantDone)
}
if got.Meta.StopReason != string(tt.wantReason) {
t.Fatalf("stop reason = %q, want %q", got.Meta.StopReason, tt.wantReason)
}
if (got.Error != nil) != tt.wantError {
t.Fatalf("error present = %v, want %v", got.Error != nil, tt.wantError)
}
if got.Hint != tt.wantHint {
t.Fatalf("hint = %q, want %q", got.Hint, tt.wantHint)
}
})
}
}
func TestReadFailureErrorWireShapeDoesNotSerializeCause(t *testing.T) {
contract := mustReadContract(t, "im +chat-list")
session, err := NewReadSession(contract, ReadOptions{FullRead: true})
if err != nil {
t.Fatal(err)
}
secret := "raw-server-cause-must-not-leak"
cause := errs.NewNetworkError(errs.SubtypeNetworkTransport, "request failed").
WithRetryable().
WithCause(assertionError(secret))
session.ObservePagination(client.PaginationStatus{
PagesFetched: 1,
HasMore: true,
NextPageToken: "opaque-token",
StopReason: client.StopReasonTransportError,
Cause: cause,
})
result, err := session.Finalize(map[string]any{"items": []any{"kept"}})
if err != nil {
t.Fatal(err)
}
wire, err := json.Marshal(result.Error)
if err != nil {
t.Fatal(err)
}
if string(wire) == "" || containsAny(string(wire), secret, "opaque-token") {
t.Fatalf("unsafe error wire: %s", wire)
}
}
func TestReadFinalizeErrorRetryMatrix(t *testing.T) {
contract := mustReadContract(t, "im +messages-mget")
tests := []struct {
name string
err error
wantRetryable bool
}{
{
name: "transport",
err: errs.NewNetworkError(errs.SubtypeNetworkTransport, "connection reset"),
wantRetryable: true,
},
{
name: "server error",
err: errs.NewAPIError(errs.SubtypeServerError, "upstream failed"),
wantRetryable: true,
},
{
name: "rate limit is not authorized",
err: errs.NewAPIError(errs.SubtypeRateLimit, "too many requests").WithRetryable(),
wantRetryable: false,
},
{
name: "permission",
err: errs.NewPermissionError(errs.SubtypeMissingScope, "missing scope"),
wantRetryable: false,
},
{
name: "not found",
err: errs.NewAPIError(errs.SubtypeNotFound, "missing"),
wantRetryable: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
session, err := NewReadSession(contract, ReadOptions{})
if err != nil {
t.Fatal(err)
}
got := session.FinalizeError(tt.err)
problem, ok := errs.ProblemOf(got)
if !ok {
t.Fatalf("FinalizeError returned untyped error %T: %v", got, got)
}
if problem.Retryable != tt.wantRetryable {
t.Fatalf("Retryable = %v, want %v: %#v", problem.Retryable, tt.wantRetryable, problem)
}
})
}
}
func TestPagedReadNormalizesRateLimitToNonRetryable(t *testing.T) {
contract := mustReadContract(t, "im +chat-list")
session, err := NewReadSession(contract, ReadOptions{FullRead: true})
if err != nil {
t.Fatal(err)
}
rateLimit := errs.NewAPIError(errs.SubtypeRateLimit, "too many requests").WithRetryable()
session.ObservePagination(client.PaginationStatus{
PagesFetched: 1,
HasMore: true,
StopReason: client.StopReasonAPIError,
Cause: rateLimit,
})
result, err := session.Finalize(map[string]any{"items": []any{"kept"}})
if err != nil {
t.Fatal(err)
}
if result.Error == nil {
t.Fatal("expected typed partial read error")
}
if result.Error.Retryable {
t.Fatalf("429/rate_limit must not authorize retry: %#v", result.Error)
}
}
func TestSearchMaterializationControlsFinalCompleteness(t *testing.T) {
contract := mustReadContract(t, "im +messages-search")
tests := []struct {
name string
status MaterializationStatus
wantOK bool
wantComplete bool
wantHint string
}{
{
name: "complete",
status: MaterializationStatus{
RequestedIDs: []string{"om_a", "om_b"},
ResolvedIDs: []string{"om_a", "om_b"},
},
wantOK: true,
wantComplete: true,
wantHint: "Results are ready to use. Use message_id/file_key directly; do not call messages-mget.",
},
{
name: "missing details",
status: MaterializationStatus{
RequestedIDs: []string{"om_a", "om_b"},
ResolvedIDs: []string{"om_a"},
MissingMessageIDs: []string{"om_b"},
},
wantOK: false,
wantComplete: false,
wantHint: "The search is incomplete. Query only materialization.missing_message_ids with im +messages-mget.",
},
{
name: "unresolved hit",
status: MaterializationStatus{
RequestedIDs: []string{"om_a"},
ResolvedIDs: []string{"om_a"},
UnresolvedHitCount: 1,
},
wantOK: false,
wantComplete: false,
wantHint: "The search is incomplete and cannot be safely recovered by message ID. Narrow the query before retrying.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
session, err := NewReadSession(contract, ReadOptions{FullRead: true})
if err != nil {
t.Fatal(err)
}
session.ObservePagination(client.PaginationStatus{PagesFetched: 2, StopReason: client.StopReasonExhausted})
session.ObserveMaterialization(tt.status)
result, err := session.Finalize(map[string]any{"messages": []any{map[string]any{"message_id": "om_a"}}})
if err != nil {
t.Fatal(err)
}
if result.OK != tt.wantOK || result.Meta == nil || result.Meta.Complete == nil ||
*result.Meta.Complete != tt.wantComplete {
t.Fatalf("result = %#v, want OK/complete %v/%v", result, tt.wantOK, tt.wantComplete)
}
if result.Hint != tt.wantHint {
t.Fatalf("hint = %q, want %q", result.Hint, tt.wantHint)
}
data := result.Data.(map[string]any)
ledger, ok := data["materialization"].(map[string]any)
if !ok {
t.Fatalf("materialization ledger missing: %#v", data)
}
wantStatus := "partial"
if tt.wantComplete {
wantStatus = "complete"
}
if ledger["status"] != wantStatus {
t.Fatalf("materialization status = %q, want %q", ledger["status"], wantStatus)
}
})
}
}
func TestSearchMaterializationRequiredButUnobservedFailsClosed(t *testing.T) {
contract := mustReadContract(t, "im +messages-search")
session, err := NewReadSession(contract, ReadOptions{FullRead: true})
if err != nil {
t.Fatal(err)
}
session.ObservePagination(client.PaginationStatus{PagesFetched: 1, StopReason: client.StopReasonExhausted})
_, err = session.Finalize(map[string]any{"messages": []any{}})
problem, ok := errs.ProblemOf(err)
if !ok || problem.Subtype != errs.SubtypeInvalidResponse {
t.Fatalf("error = %T %v, want invalid_response", err, err)
}
}
func TestSearchMaterializationDoesNotOverwritePaginationFailure(t *testing.T) {
contract := mustReadContract(t, "im +messages-search")
session, err := NewReadSession(contract, ReadOptions{FullRead: true})
if err != nil {
t.Fatal(err)
}
pageErr := errs.NewNetworkError(errs.SubtypeNetworkTransport, "later page failed")
session.ObservePagination(client.PaginationStatus{
PagesFetched: 1,
HasMore: true,
NextPageToken: "next",
StopReason: client.StopReasonTransportError,
Cause: pageErr,
})
session.ObserveMaterialization(MaterializationStatus{
RequestedIDs: []string{"om_a", "om_b"},
ResolvedIDs: []string{"om_a"},
MissingMessageIDs: []string{"om_b"},
})
result, err := session.Finalize(map[string]any{"messages": []any{map[string]any{"message_id": "om_a"}}})
if err != nil {
t.Fatal(err)
}
if result.OK || result.ExitCode != output.ExitNetwork || result.Cause != pageErr {
t.Fatalf("pagination failure was overwritten: %#v", result)
}
if result.Error == nil || !result.Error.Retryable {
t.Fatalf("pagination problem was not preserved: %#v", result.Error)
}
for _, want := range []string{hintReadFailed, "materialization.missing_message_ids"} {
if !strings.Contains(result.Hint, want) {
t.Fatalf("combined hint = %q, want %q", result.Hint, want)
}
}
}
func TestSearchMaterializationDoesNotExposeUnexpectedIDs(t *testing.T) {
status := MaterializationStatus{
RequestedIDs: []string{"om_requested"},
ResolvedIDs: []string{"om_requested"},
UnexpectedMessageCount: 1,
}
wire, err := json.Marshal(status.ledger())
if err != nil {
t.Fatal(err)
}
if containsAny(string(wire), "om_requested", "om_unknown_secret") {
t.Fatalf("ledger leaked internal IDs: %s", wire)
}
}
func TestSearchEmptyResultAddsNonExistenceHint(t *testing.T) {
contract := mustReadContract(t, "im +chat-search")
session, err := NewReadSession(contract, ReadOptions{})
if err != nil {
t.Fatal(err)
}
session.ObservePagination(client.PaginationStatus{PagesFetched: 1, StopReason: client.StopReasonExhausted})
result, err := session.Finalize(map[string]any{"chats": []any{}})
if err != nil {
t.Fatal(err)
}
if result.Meta == nil || result.Meta.Complete == nil || !*result.Meta.Complete {
t.Fatalf("expected exhausted result to be complete: %#v", result.Meta)
}
const wantHint = "The search was exhausted, but an empty search result does not prove that the resource does not exist."
if result.Hint != wantHint {
t.Fatalf("hint = %q, want %q", result.Hint, wantHint)
}
}
func TestEntityAndMaterializeDoNotInventPagination(t *testing.T) {
for _, key := range []ContractKey{"im chat.nickname get", "im +messages-resources-download"} {
t.Run(string(key), func(t *testing.T) {
contract := mustReadContract(t, key)
session, err := NewReadSession(contract, ReadOptions{})
if err != nil {
t.Fatal(err)
}
result, err := session.Finalize(map[string]any{"nickname": ""})
if err != nil {
t.Fatal(err)
}
if !result.OK || result.Meta != nil || result.ExitCode != 0 {
t.Fatalf("unexpected finite result: %#v", result)
}
})
}
}
func TestUnknownReadStrategyFailsClosed(t *testing.T) {
_, err := NewReadSession(Contract{
Key: "im future read",
Strategy: Strategy{Kind: StrategyKind("future_read")},
}, ReadOptions{})
if err == nil || !errs.IsInternal(err) {
t.Fatalf("expected typed internal error, got %v", err)
}
}
func mustReadContract(t *testing.T, key ContractKey) Contract {
t.Helper()
contract, ok := Lookup(key)
if !ok {
t.Fatalf("missing contract %q", key)
}
return contract
}
type assertionError string
func (e assertionError) Error() string { return string(e) }
func containsAny(s string, values ...string) bool {
for _, value := range values {
if value != "" && stringContains(s, value) {
return true
}
}
return false
}
func stringContains(s, substr string) bool {
for i := 0; i+len(substr) <= len(s); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}

View File

@@ -0,0 +1,22 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import "github.com/larksuite/cli/internal/imcontract/catalog"
func Lookup(key ContractKey) (Contract, bool) {
return catalog.Lookup(key)
}
func All() []Contract {
return catalog.All()
}
func ValidateRegistry() error {
return catalog.ValidateRegistry()
}
func stringsFrom(field string) evidenceSpec {
return evidenceSpec{Shape: evidenceStrings, Field: field}
}

View File

@@ -0,0 +1,141 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"slices"
"testing"
)
func TestWriteRegistryCoverage(t *testing.T) {
counts := map[StrategyKind]int{}
total := 0
for _, contract := range All() {
if contract.Strategy.Kind.IsWrite() {
counts[contract.Strategy.Kind]++
total++
}
}
if total != 36 {
t.Fatalf("write contracts = %d, want 36", total)
}
want := map[StrategyKind]int{
AuthoritativeAckKind: 9,
RequiredResultKind: 12,
BatchPartialKind: 11,
RequiredResultBatchPartialKind: 1,
ResponseSetAssertionKind: 2,
AcceptanceOnlyKind: 1,
}
for kind, n := range want {
if counts[kind] != n {
t.Errorf("%s = %d, want %d", kind, counts[kind], n)
}
}
if err := ValidateRegistry(); err != nil {
t.Fatal(err)
}
wantKeys := []ContractKey{
"im +chat-create", "im +chat-update", "im +feed-shortcut-create",
"im +feed-shortcut-remove", "im +flag-cancel", "im +flag-create",
"im +messages-reply", "im +messages-send",
"im chat.managers add_managers", "im chat.managers delete_managers",
"im chat.members create", "im chat.members delete",
"im chat.moderation update", "im chat.nickname delete",
"im chat.nickname update", "im chat.user_setting batch_update",
"im chats create", "im chats link", "im chats update",
"im feed.groups batch_add_item", "im feed.groups batch_remove_item",
"im feed.groups create", "im feed.groups delete", "im feed.groups update",
"im images create", "im messages delete", "im messages forward",
"im messages merge_forward", "im messages urgent_app",
"im messages urgent_phone", "im messages urgent_sms", "im pins create",
"im pins delete", "im reactions create", "im reactions delete",
"im threads forward",
}
gotKeys := make([]ContractKey, 0, len(All()))
for _, c := range All() {
if c.Strategy.Kind.IsWrite() {
gotKeys = append(gotKeys, c.Key)
}
}
if !slices.Equal(gotKeys, wantKeys) {
t.Fatalf("write registry keys differ:\ngot %v\nwant %v", gotKeys, wantKeys)
}
}
func TestModerationAcceptanceOnlyContract(t *testing.T) {
c, ok := Lookup("im chat.moderation update")
if !ok {
t.Fatal("moderation contract missing")
}
if c.Strategy.Kind != AcceptanceOnlyKind || c.ReplayMode != ReplayForbidden ||
c.HelpPolicy != HelpAcceptanceOnly {
t.Fatalf("unexpected moderation contract: %#v", c)
}
}
func TestReadRegistryCoverage(t *testing.T) {
counts := map[StrategyKind]int{}
var gotKeys []ContractKey
for _, contract := range All() {
if !contract.Strategy.Kind.IsRead() {
continue
}
counts[contract.Strategy.Kind]++
gotKeys = append(gotKeys, contract.Key)
}
if len(gotKeys) != 24 {
t.Fatalf("read contracts = %d, want 24", len(gotKeys))
}
wantCounts := map[StrategyKind]int{
EntityReadKind: 8,
CollectionReadKind: 13,
SearchReadKind: 2,
MaterializeReadKind: 1,
}
for kind, want := range wantCounts {
if got := counts[kind]; got != want {
t.Errorf("%s = %d, want %d", kind, got, want)
}
}
wantKeys := []ContractKey{
"im +chat-list",
"im +chat-members-list",
"im +chat-messages-list",
"im +chat-search",
"im +feed-group-list",
"im +feed-group-list-item",
"im +feed-group-query-item",
"im +feed-shortcut-list",
"im +flag-list",
"im +messages-mget",
"im +messages-resources-download",
"im +messages-search",
"im +threads-messages-list",
"im chat.members bots",
"im chat.members get",
"im chat.moderation get",
"im chat.nickname get",
"im chat.user_setting batch_query",
"im chats get",
"im feed.groups batch_query",
"im messages read_users",
"im pins list",
"im reactions batch_query",
"im reactions list",
}
if !slices.Equal(gotKeys, wantKeys) {
t.Fatalf("read registry keys differ:\ngot %v\nwant %v", gotKeys, wantKeys)
}
}
func TestModerationGetUsesCollectionCompletenessContract(t *testing.T) {
c, ok := Lookup("im chat.moderation get")
if !ok {
t.Fatal("moderation get contract missing")
}
if c.Strategy.Kind != CollectionReadKind || c.HelpPolicy != HelpCompleteness {
t.Fatalf("unexpected moderation get contract: %#v", c)
}
}

View File

@@ -0,0 +1,179 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"errors"
"strings"
"github.com/larksuite/cli/errs"
)
type Session struct {
contract Contract
requested []ledgerItem
hasIdempotencyKey bool
facts []Fact
}
func NewSession(contract Contract) *Session {
return &Session{contract: contract}
}
func (s *Session) Contract() Contract {
return s.contract
}
func (s *Session) ObserveRequest(body map[string]any) error {
if spec := s.contract.Strategy.Request; spec.Field != "" {
evidence := extract(body, spec)
if !evidence.present || evidence.selectedCount == 0 ||
evidence.rejectedCount != 0 ||
evidence.rawCount != evidence.selectedCount+evidence.rejectedCount {
return errs.NewValidationError(
errs.SubtypeInvalidArgument,
"IM write request field %q has an unsupported shape",
spec.Field,
)
}
s.requested = uniqueItems(append(s.requested, evidence.items...))
}
if strings.TrimSpace(stableID(body["uuid"])) != "" {
s.hasIdempotencyKey = true
}
return nil
}
func (s *Session) ObserveResponse(_ map[string]any) {}
func (s *Session) RecordFact(f Fact) {
switch f.Kind {
case FactMediaPreuploadPerformed, FactWriteAttempted:
if s.hasFact(f.Kind) {
return
}
s.facts = append(s.facts, Fact{Kind: f.Kind})
case FactFlagFeedLayerPending:
s.facts = append(s.facts, Fact{Kind: f.Kind, Item: "feed"})
}
}
func (s *Session) hasFact(kind FactKind) bool {
for _, fact := range s.facts {
if fact.Kind == kind {
return true
}
}
return false
}
func (s *Session) FinalizeSuccess(data any) (Result, error) {
s.RecordFact(Fact{Kind: FactWriteAttempted})
switch s.contract.Strategy.Kind {
case AuthoritativeAckKind:
return Result{OK: true, Data: data}, nil
case RequiredResultKind:
if !requiredResultPresent(data, s.contract.Strategy.Required) {
return Result{}, s.FinalizeError(invalidRequiredResult(requiredLabel(s.contract.Strategy.Required)))
}
if supportsMessageMentionResult(s.contract.Key) {
result, err := finalizeMessageMentions(data)
if err != nil {
return Result{}, s.FinalizeError(err)
}
return result, nil
}
return Result{OK: true, Data: data}, nil
case BatchPartialKind:
return finalizeBatch(s, data)
case RequiredResultBatchPartialKind:
result, err := finalizeBatch(s, data)
if err != nil {
return Result{}, err
}
if !result.OK {
return result, nil
}
if !requiredResultPresent(data, s.contract.Strategy.Required) {
return Result{}, s.FinalizeError(invalidRequiredResult(requiredLabel(s.contract.Strategy.Required)))
}
return result, nil
case ResponseSetAssertionKind:
return finalizeAssertion(s, data)
case AcceptanceOnlyKind:
m, err := checkedResponse(data)
if err != nil {
return Result{}, err
}
m["completion"] = map[string]any{
"status": "accepted_unverified",
"final_state_verified": false,
"retry_scope": "none",
}
return Result{OK: true, Data: m, Hint: s.contract.HelpPolicy.Text()}, nil
default:
return Result{}, errs.NewInternalError(
errs.SubtypeInvalidResponse,
"unsupported IM write contract strategy %q",
s.contract.Strategy.Kind,
)
}
}
func supportsMessageMentionResult(key ContractKey) bool {
return key == "im +messages-send" || key == "im +messages-reply"
}
func requiredLabel(spec requiredSpec) string {
if spec.Child == "" {
return spec.Field
}
return spec.Field + "/" + spec.Child
}
func (s *Session) FinalizeError(err error) error {
problem, ok := errs.ProblemOf(err)
if !ok {
return err
}
if problem.Subtype == errs.SubtypeRateLimit {
problem.Retryable = false
problem.Hint = ""
return err
}
transient := problem.Category == errs.CategoryNetwork ||
(problem.Category == errs.CategoryAPI && problem.Retryable)
if !transient && problem.Subtype != errs.SubtypeInvalidResponse {
return err
}
if !s.hasFact(FactWriteAttempted) {
return err
}
var evidenceErr *invalidEvidenceError
if errors.As(err, &evidenceErr) {
problem.Retryable = false
problem.Hint = hintUnsafeEvidence
return err
}
mode := s.contract.ReplayMode
if s.hasFact(FactMediaPreuploadPerformed) {
mode = ReplayForbidden
}
switch mode {
case ReplaySafe:
problem.Retryable = true
problem.Hint = hintReplaySafe
case ReplaySameIdempotencyKey:
if s.hasIdempotencyKey {
problem.Retryable = true
problem.Hint = hintSameKey
return err
}
fallthrough
default:
problem.Retryable = false
problem.Hint = hintReplayForbidden
}
return err
}

View File

@@ -0,0 +1,76 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
// Package imcontract evaluates IM command completion evidence.
package imcontract
import "github.com/larksuite/cli/internal/imcontract/catalog"
type ContractKey = catalog.ContractKey
type StrategyKind = catalog.StrategyKind
type ReplayMode = catalog.ReplayMode
type PartialRecoveryMode = catalog.PartialRecoveryMode
type AssertionMode = catalog.AssertionMode
type Strategy = catalog.Strategy
type HelpPolicy = catalog.HelpPolicy
type Contract = catalog.Contract
type requiredSpec = catalog.RequiredSpec
type evidenceSpec = catalog.EvidenceSpec
const (
EntityReadKind = catalog.EntityReadKind
CollectionReadKind = catalog.CollectionReadKind
SearchReadKind = catalog.SearchReadKind
MaterializeReadKind = catalog.MaterializeReadKind
AuthoritativeAckKind = catalog.AuthoritativeAckKind
RequiredResultKind = catalog.RequiredResultKind
BatchPartialKind = catalog.BatchPartialKind
RequiredResultBatchPartialKind = catalog.RequiredResultBatchPartialKind
ResponseSetAssertionKind = catalog.ResponseSetAssertionKind
AcceptanceOnlyKind = catalog.AcceptanceOnlyKind
ReplayForbidden = catalog.ReplayForbidden
ReplaySafe = catalog.ReplaySafe
ReplaySameIdempotencyKey = catalog.ReplaySameIdempotencyKey
PartialRecoveryWholeRequest = catalog.PartialRecoveryWholeRequest
PartialRecoveryFailedItemsOnly = catalog.PartialRecoveryFailedItemsOnly
AssertRequestedPresent = catalog.AssertRequestedPresent
AssertRequestedAbsent = catalog.AssertRequestedAbsent
requiredTopString = catalog.RequiredTopString
requiredTopObject = catalog.RequiredTopObject
requiredNestedString = catalog.RequiredNestedString
evidenceStrings = catalog.EvidenceStrings
evidenceObjects = catalog.EvidenceObjects
evidenceNestedObjects = catalog.EvidenceNestedObjects
evidenceFeedObjects = catalog.EvidenceFeedObjects
evidenceNestedFeedObjects = catalog.EvidenceNestedFeedObjects
evidenceStatusObjects = catalog.EvidenceStatusObjects
HelpCompleteness = catalog.HelpCompleteness
HelpAcceptanceOnly = catalog.HelpAcceptanceOnly
)
type FactKind string
const (
FactMediaPreuploadPerformed FactKind = "media_preupload_performed"
FactFlagFeedLayerPending FactKind = "flag_feed_layer_pending"
FactWriteAttempted FactKind = "write_attempted"
)
type Fact struct {
Kind FactKind
Item string
}
type Result struct {
OK bool
Data any
Hint string
ExitCode int
}

View File

@@ -0,0 +1,199 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"fmt"
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/output"
)
const (
hintReplayForbidden = "The write result is unknown. Do not replay the original request."
hintReplaySafe = "The write result is unknown. Retrying the original request is safe."
hintSameKey = "The write result is unknown. Retry only with the same idempotency key."
hintUnsafeEvidence = "The server response could not be safely mapped to the original request. Do not retry the write based on this response."
)
func invalidRequiredResult(field string) error {
return errs.NewInternalError(errs.SubtypeInvalidResponse,
"successful response is missing required field %q", field)
}
type invalidEvidenceError struct {
cause error
}
func (e *invalidEvidenceError) Error() string {
return e.cause.Error()
}
func (e *invalidEvidenceError) Unwrap() error {
return e.cause
}
func invalidEvidence(field string) error {
return &invalidEvidenceError{
cause: errs.NewInternalError(
errs.SubtypeInvalidResponse,
"response evidence in %q cannot be mapped to the original request",
field,
).WithHint(hintUnsafeEvidence),
}
}
func requiredResultPresent(data any, spec requiredSpec) bool {
root, ok := data.(map[string]any)
if !ok {
return false
}
switch spec.Shape {
case requiredTopString:
return nonEmptyString(root[spec.Field]) != ""
case requiredTopObject:
object, ok := root[spec.Field].(map[string]any)
return ok && len(object) > 0
case requiredNestedString:
object, ok := root[spec.Field].(map[string]any)
return ok && nonEmptyString(object[spec.Child]) != ""
default:
return false
}
}
func checkedResponse(data any) (map[string]any, error) {
root, ok := data.(map[string]any)
if !ok {
return nil, invalidEvidence("response")
}
return root, nil
}
func validateEvidence(result extraction, requested []ledgerItem, field string, requireRequested bool) error {
if !result.present {
return nil
}
if result.rejectedCount != 0 ||
result.rawCount != result.selectedCount+result.rejectedCount {
return invalidEvidence(field)
}
if !requireRequested {
return nil
}
requestedSet := make(map[string]struct{}, len(requested))
for _, item := range requested {
requestedSet[item.key] = struct{}{}
}
for _, item := range result.items {
if _, ok := requestedSet[item.key]; !ok {
return invalidEvidence(field)
}
}
return nil
}
func finalizeBatch(s *Session, data any) (Result, error) {
root, err := checkedResponse(data)
if err != nil {
return Result{}, err
}
requested := append([]ledgerItem{}, s.requested...)
failed := make([]ledgerItem, 0)
for _, spec := range s.contract.Strategy.Failures {
evidence := extract(root, spec)
if err := validateEvidence(evidence, requested, spec.Field, true); err != nil {
return Result{}, err
}
failed = append(failed, evidence.items...)
}
responsePending := make([]ledgerItem, 0)
for _, spec := range s.contract.Strategy.Pending {
evidence := extract(root, spec)
if err := validateEvidence(evidence, requested, spec.Field, true); err != nil {
return Result{}, err
}
responsePending = append(responsePending, evidence.items...)
}
syntheticPending := make([]ledgerItem, 0)
if s.hasFact(FactFlagFeedLayerPending) {
syntheticPending = append(syntheticPending, ledgerItem{key: "feed", value: "feed"})
}
if spec := s.contract.Strategy.ResultLedger; spec != nil {
evidence := extract(root, *spec)
if err := validateEvidence(evidence, nil, spec.Field, false); err != nil {
return Result{}, err
}
requested = append(requested, evidence.items...)
failed = append(failed, statusFailures(root, *spec)...)
}
// Response pending can only classify an original request. Synthetic pending
// represents a logical sub-request performed by a shortcut.
requested = append(requested, syntheticPending...)
pending := append(responsePending, syntheticPending...)
ledger := completion(requested, failed, pending, s.contract.PartialRecovery)
root["completion"] = ledger
result := Result{OK: ledger.Status == "complete", Data: root}
if !result.OK {
result.ExitCode = output.ExitAPI
}
return result, nil
}
func statusFailures(root map[string]any, spec evidenceSpec) []ledgerItem {
values, _ := root[spec.Field].([]any)
failed := make([]ledgerItem, 0)
for _, value := range values {
object, _ := value.(map[string]any)
if fmt.Sprint(object["status"]) != "failed" {
continue
}
item, ok := stringItem(object[spec.IDField])
if ok {
failed = append(failed, item)
}
}
return failed
}
func finalizeAssertion(s *Session, data any) (Result, error) {
root, err := checkedResponse(data)
if err != nil {
return Result{}, err
}
actual := make(map[string]struct{})
responseSetPresent := false
for _, spec := range s.contract.Strategy.ResponseSets {
evidence := extract(root, spec)
if err := validateEvidence(evidence, nil, spec.Field, false); err != nil {
return Result{}, err
}
responseSetPresent = responseSetPresent || evidence.present
for _, item := range evidence.items {
actual[item.key] = struct{}{}
}
}
if !responseSetPresent {
return Result{}, invalidEvidence("response_sets")
}
failed := make([]ledgerItem, 0)
for _, item := range s.requested {
_, exists := actual[item.key]
if (s.contract.Strategy.Assertion == AssertRequestedPresent && !exists) ||
(s.contract.Strategy.Assertion == AssertRequestedAbsent && exists) {
failed = append(failed, item)
}
}
ledger := completion(s.requested, failed, nil, PartialRecoveryFailedItemsOnly)
root["completion"] = ledger
result := Result{OK: ledger.Status == "complete", Data: root}
if !result.OK {
result.ExitCode = output.ExitAPI
}
return result, nil
}

View File

@@ -0,0 +1,573 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package imcontract
import (
"encoding/json"
"strings"
"testing"
"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/internal/output"
)
func TestRequiredResult(t *testing.T) {
c, _ := Lookup("im +messages-send")
for _, data := range []map[string]any{{}, {"message_id": ""}} {
s := NewSession(c)
_, err := s.FinalizeSuccess(data)
if err == nil {
t.Fatalf("expected missing result error for %#v", data)
}
p, _ := errs.ProblemOf(err)
if p.Category != errs.CategoryInternal || p.Subtype != errs.SubtypeInvalidResponse {
t.Fatalf("problem = %#v", p)
}
if output.ExitCodeOf(err) != output.ExitInternal {
t.Fatalf("exit = %d", output.ExitCodeOf(err))
}
}
s := NewSession(c)
got, err := s.FinalizeSuccess(map[string]any{"message_id": "om_x"})
if err != nil || !got.OK {
t.Fatalf("valid result rejected: %#v %v", got, err)
}
}
func TestBatchPartialLedger(t *testing.T) {
c, _ := Lookup("im messages urgent_app")
s := NewSession(c)
s.ObserveRequest(map[string]any{"user_id_list": []any{"ou_a", "ou_b"}})
got, err := s.FinalizeSuccess(map[string]any{"invalid_user_id_list": []any{"ou_b"}})
if err != nil {
t.Fatal(err)
}
if got.OK || got.ExitCode != output.ExitAPI {
t.Fatalf("result = %#v", got)
}
completion := got.Data.(map[string]any)["completion"].(Completion)
if completion.Status != "partial" || completion.SucceededCount != 1 || completion.FailedCount != 1 {
t.Fatalf("completion = %#v", completion)
}
if len(completion.FailedItems) != 1 || completion.FailedItems[0] != "ou_b" {
t.Fatalf("failed items = %#v", completion.FailedItems)
}
}
func TestBatchPendingIsNotCountedAsSucceeded(t *testing.T) {
c, _ := Lookup("im chat.members create")
s := NewSession(c)
s.ObserveRequest(map[string]any{"id_list": []any{"ou_a", "ou_b"}})
got, err := s.FinalizeSuccess(map[string]any{"pending_approval_id_list": []any{"ou_b"}})
if err != nil {
t.Fatal(err)
}
completion := got.Data.(map[string]any)["completion"].(Completion)
if completion.SucceededCount != 1 || completion.PendingCount != 1 || completion.RetryScope != "none" {
t.Fatalf("completion = %#v", completion)
}
}
func TestResponsePendingCannotExpandRequestedLedger(t *testing.T) {
c, _ := Lookup("im chat.members create")
s := NewSession(c)
s.ObserveRequest(map[string]any{
"id_list": []any{"ou_a", "ou_b"},
})
got, err := s.FinalizeSuccess(map[string]any{
"pending_approval_id_list": []any{"ou_unknown"},
})
if err == nil {
t.Fatalf("unknown response pending was accepted: %#v", got)
}
assertUnsafeEvidenceError(t, err)
}
func TestSyntheticFlagPendingExpandsLogicalRequest(t *testing.T) {
c, _ := Lookup("im +flag-cancel")
s := NewSession(c)
s.RecordFact(Fact{Kind: FactFlagFeedLayerPending})
got, err := s.FinalizeSuccess(map[string]any{"results": []any{
map[string]any{"flag_type": "message", "status": "ok"},
}})
if err != nil {
t.Fatal(err)
}
completion := got.Data.(map[string]any)["completion"].(Completion)
if completion.RequestedCount != 2 || completion.SucceededCount != 1 ||
completion.FailedCount != 0 || completion.PendingCount != 1 ||
len(completion.PendingItems) != 1 || completion.PendingItems[0] != "feed" {
t.Fatalf("synthetic pending did not expand logical request: %#v", completion)
}
}
func TestRequiredResultBatchPartialPrioritizesLedger(t *testing.T) {
c, _ := Lookup("im messages merge_forward")
s := NewSession(c)
s.ObserveRequest(map[string]any{"message_id_list": []any{"om_a", "om_b"}})
got, err := s.FinalizeSuccess(map[string]any{"invalid_message_id_list": []any{"om_b"}})
if err != nil || got.OK || got.ExitCode != output.ExitAPI {
t.Fatalf("partial result = %#v, err=%v", got, err)
}
s = NewSession(c)
s.ObserveRequest(map[string]any{"message_id_list": []any{"om_a"}})
_, err = s.FinalizeSuccess(map[string]any{})
if err == nil {
t.Fatal("missing merged message_id must fail when no partial result exists")
}
}
func TestManagerResponseSetAssertions(t *testing.T) {
for _, tc := range []struct {
key ContractKey
response map[string]any
wantOK bool
}{
{"im chat.managers add_managers", map[string]any{"chat_managers": []any{"ou_a"}}, true},
{"im chat.managers add_managers", map[string]any{"chat_managers": []any{}}, false},
{"im chat.managers delete_managers", map[string]any{"chat_managers": []any{}}, true},
{"im chat.managers delete_managers", map[string]any{"chat_managers": []any{"ou_a"}}, false},
} {
c, _ := Lookup(tc.key)
s := NewSession(c)
s.ObserveRequest(map[string]any{"manager_ids": []any{"ou_a"}})
got, err := s.FinalizeSuccess(tc.response)
if err != nil || got.OK != tc.wantOK {
t.Errorf("%s response=%v: got %#v, err=%v", tc.key, tc.response, got, err)
}
}
}
func TestManagerResponseSetAssertionsRequirePresentEvidence(t *testing.T) {
for _, key := range []ContractKey{
"im chat.managers add_managers",
"im chat.managers delete_managers",
} {
t.Run(string(key), func(t *testing.T) {
c, _ := Lookup(key)
s := NewSession(c)
s.ObserveRequest(map[string]any{"manager_ids": []any{"ou_a"}})
got, err := s.FinalizeSuccess(map[string]any{})
if err == nil {
t.Fatalf("missing response sets were accepted: %#v", got)
}
assertUnsafeEvidenceError(t, err)
})
}
}
func TestModerationAcceptedUnverified(t *testing.T) {
c, _ := Lookup("im chat.moderation update")
got, err := NewSession(c).FinalizeSuccess(map[string]any{})
if err != nil {
t.Fatal(err)
}
completion := got.Data.(map[string]any)["completion"].(map[string]any)
if completion["status"] != "accepted_unverified" || completion["final_state_verified"] != false {
t.Fatalf("completion = %#v", completion)
}
if got.Hint != HelpAcceptanceOnly.Text() {
t.Fatalf("hint = %q", got.Hint)
}
}
func TestReplaySafety(t *testing.T) {
unknown := errs.NewNetworkError(errs.SubtypeNetworkTransport, "request failed").WithHint("untrusted upstream hint")
c, _ := Lookup("im +messages-send")
s := NewSession(c)
s.ObserveRequest(map[string]any{"uuid": "stable-key"})
s.RecordFact(Fact{Kind: FactWriteAttempted})
got := s.FinalizeError(unknown)
p, _ := errs.ProblemOf(got)
if !p.Retryable || p.Hint != hintSameKey {
t.Fatalf("same-key problem = %#v", p)
}
unknown = errs.NewNetworkError(errs.SubtypeNetworkTransport, "request failed").WithHint("untrusted upstream hint")
s = NewSession(c)
s.ObserveRequest(map[string]any{"uuid": "stable-key"})
s.RecordFact(Fact{Kind: FactWriteAttempted})
s.RecordFact(Fact{Kind: FactMediaPreuploadPerformed})
got = s.FinalizeError(unknown)
p, _ = errs.ProblemOf(got)
if p.Retryable || p.Hint != hintReplayForbidden {
t.Fatalf("preupload problem = %#v", p)
}
validation := errs.NewValidationError(errs.SubtypeInvalidArgument, "bad flag")
got = NewSession(c).FinalizeError(validation)
p, _ = errs.ProblemOf(got)
if p.Retryable || p.Hint != "" {
t.Fatalf("validation problem was broadened: %#v", p)
}
unknown = errs.NewNetworkError(errs.SubtypeNetworkTransport, "request failed").WithHint("untrusted upstream hint")
c, _ = Lookup("im +feed-shortcut-create")
s = NewSession(c)
s.RecordFact(Fact{Kind: FactWriteAttempted})
got = s.FinalizeError(unknown)
p, _ = errs.ProblemOf(got)
if !p.Retryable || p.Hint != hintReplaySafe {
t.Fatalf("safe replay problem = %#v", p)
}
preflight := errs.NewNetworkError(errs.SubtypeNetworkTransport, "lookup failed").
WithRetryable().
WithHint("specify --item-type explicitly")
c, _ = Lookup("im +flag-create")
got = NewSession(c).FinalizeError(preflight)
p, _ = errs.ProblemOf(got)
if !p.Retryable || p.Hint != "specify --item-type explicitly" {
t.Fatalf("preflight problem was rewritten: %#v", p)
}
}
func TestWriteRateLimitNeverAuthorizesReplay(t *testing.T) {
for _, key := range []ContractKey{
"im +feed-shortcut-create",
"im +messages-send",
} {
t.Run(string(key), func(t *testing.T) {
contract, _ := Lookup(key)
session := NewSession(contract)
session.ObserveRequest(map[string]any{"uuid": "stable-key"})
session.RecordFact(Fact{Kind: FactWriteAttempted})
rateLimit := errs.NewAPIError(errs.SubtypeRateLimit, "too many requests").
WithRetryable().
WithHint("retry later")
got := session.FinalizeError(rateLimit)
problem, ok := errs.ProblemOf(got)
if !ok {
t.Fatalf("FinalizeError returned untyped error %T: %v", got, got)
}
if problem.Retryable || problem.Hint != "" {
t.Fatalf("rate limit authorized replay for %s: %#v", key, problem)
}
})
}
}
func TestBatchPartialRecoveryMatrix(t *testing.T) {
tests := []struct {
name string
command ContractKey
request map[string]any
response map[string]any
fact *Fact
wantScope string
}{
{
name: "pending always forbids retry",
command: "im +flag-cancel",
response: map[string]any{"results": []any{
map[string]any{"flag_type": "message", "status": "ok"},
}},
fact: &Fact{Kind: FactFlagFeedLayerPending},
wantScope: "none",
},
{
name: "whole request recovery",
command: "im +feed-shortcut-create",
request: map[string]any{"shortcuts": []any{
map[string]any{"feed_card_id": "oc_a"},
}},
response: map[string]any{"failed_shortcuts": []any{
map[string]any{"shortcut": map[string]any{"feed_card_id": "oc_a"}},
}},
wantScope: "whole_request",
},
{
name: "failed items only recovery",
command: "im messages urgent_app",
request: map[string]any{"user_id_list": []any{"ou_a", "ou_b"}},
response: map[string]any{"invalid_user_id_list": []any{"ou_b"}},
wantScope: "failed_items_only",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
contract, _ := Lookup(tc.command)
session := NewSession(contract)
if tc.request != nil {
if err := session.ObserveRequest(tc.request); err != nil {
t.Fatal(err)
}
}
if tc.fact != nil {
session.RecordFact(*tc.fact)
}
result, err := session.FinalizeSuccess(tc.response)
if err != nil {
t.Fatal(err)
}
completion := result.Data.(map[string]any)["completion"].(Completion)
if completion.RetryScope != tc.wantScope || result.Hint != "" {
t.Fatalf("completion=%#v hint=%q", completion, result.Hint)
}
})
}
}
func TestBatchRejectsUnmappableFailureEvidence(t *testing.T) {
for _, tc := range []struct {
name string
command ContractKey
request map[string]any
response map[string]any
}{
{
name: "all IDs missing",
command: "im chat.members create",
request: map[string]any{"id_list": []any{"ou_a"}},
response: map[string]any{"invalid_id_list": []any{map[string]any{"reason": "bad"}}},
},
{
name: "one ID missing",
command: "im chat.members create",
request: map[string]any{"id_list": []any{"ou_a", "ou_b"}},
response: map[string]any{"invalid_id_list": []any{
"ou_a", map[string]any{"reason": "bad"},
}},
},
{
name: "stable ID outside request",
command: "im chat.members create",
request: map[string]any{"id_list": []any{"ou_a"}},
response: map[string]any{"invalid_id_list": []any{"ou_unknown"}},
},
{
name: "compound feed ID missing",
command: "im feed.groups batch_add_item",
request: map[string]any{"items": []any{
map[string]any{"feed_id": "oc_a", "feed_type": "chat"},
}},
response: map[string]any{"failed_items": []any{
map[string]any{"item": map[string]any{"feed_type": "chat"}},
}},
},
{
name: "compound feed type missing",
command: "im feed.groups batch_add_item",
request: map[string]any{"items": []any{
map[string]any{"feed_id": "oc_a", "feed_type": "chat"},
}},
response: map[string]any{"failed_items": []any{
map[string]any{"item": map[string]any{"feed_id": "oc_a"}},
}},
},
} {
t.Run(tc.name, func(t *testing.T) {
c, _ := Lookup(tc.command)
s := NewSession(c)
s.ObserveRequest(tc.request)
got, err := s.FinalizeSuccess(tc.response)
if err == nil {
t.Fatalf("unmappable response was accepted: %#v", got)
}
assertUnsafeEvidenceError(t, err)
})
}
}
func TestAssertionRejectsUnmappableResponseEvidence(t *testing.T) {
c, _ := Lookup("im chat.managers add_managers")
s := NewSession(c)
s.ObserveRequest(map[string]any{"manager_ids": []any{"ou_a"}})
got, err := s.FinalizeSuccess(map[string]any{
"chat_managers": []any{map[string]any{"name": "missing ID"}},
})
if err == nil {
t.Fatalf("unmappable assertion response was accepted: %#v", got)
}
assertUnsafeEvidenceError(t, err)
}
func TestRequestEvidenceFailsClosedOnUnsupportedShapes(t *testing.T) {
c, _ := Lookup("im chat.members create")
for _, tc := range []struct {
name string
body map[string]any
}{
{name: "non-map body reaches contract as nil", body: nil},
{name: "missing collection", body: map[string]any{}},
{name: "wrong collection type", body: map[string]any{"id_list": []string{"ou_a"}}},
{name: "unmappable item", body: map[string]any{"id_list": []any{map[int]any{1: "ou_a"}}}},
} {
t.Run(tc.name, func(t *testing.T) {
err := NewSession(c).ObserveRequest(tc.body)
problem, ok := errs.ProblemOf(err)
if !ok || problem.Category != errs.CategoryValidation ||
problem.Subtype != errs.SubtypeInvalidArgument {
t.Fatalf("request evidence error = %#v, ok=%v", problem, ok)
}
})
}
}
func TestExtractionAccounting(t *testing.T) {
got := extract(map[string]any{
"ids": []any{"ou_a", map[string]any{"missing": "id"}, "ou_a"},
}, stringsFrom("ids"))
if !got.present || got.rawCount != 3 || got.selectedCount != 2 ||
got.rejectedCount != 1 || len(got.items) != 1 {
t.Fatalf("extraction = %#v", got)
}
got = extract(map[string]any{"ids": []string{"ou_a"}}, stringsFrom("ids"))
if !got.present || got.rawCount != 0 || got.selectedCount != 0 ||
got.rejectedCount != 1 || len(got.items) != 0 {
t.Fatalf("wrong-shape extraction = %#v", got)
}
}
func TestStatusLedgerRejectsUnknownStatus(t *testing.T) {
c, _ := Lookup("im +flag-cancel")
got, err := NewSession(c).FinalizeSuccess(map[string]any{"results": []any{
map[string]any{"flag_type": "message", "status": "maybe"},
}})
if err == nil {
t.Fatalf("unknown result status was accepted: %#v", got)
}
assertUnsafeEvidenceError(t, err)
}
func TestUnsafeEvidenceRemainsForbiddenAcrossFinalizeError(t *testing.T) {
c, _ := Lookup("im +feed-shortcut-create")
s := NewSession(c)
if err := s.ObserveRequest(map[string]any{"shortcuts": []any{
map[string]any{"feed_card_id": "oc_a"},
}}); err != nil {
t.Fatal(err)
}
_, err := s.FinalizeSuccess(map[string]any{"failed_shortcuts": []any{
map[string]any{"shortcut": map[string]any{"missing": "feed_card_id"}},
}})
if err == nil {
t.Fatal("malformed evidence was accepted")
}
for i := 0; i < 2; i++ {
err = s.FinalizeError(err)
assertUnsafeEvidenceError(t, err)
}
}
func assertUnsafeEvidenceError(t *testing.T, err error) {
t.Helper()
problem, ok := errs.ProblemOf(err)
if !ok || problem.Category != errs.CategoryInternal ||
problem.Subtype != errs.SubtypeInvalidResponse ||
problem.Retryable || problem.Hint != hintUnsafeEvidence {
t.Fatalf("unsafe evidence error = %#v, ok=%v", problem, ok)
}
if output.ExitCodeOf(err) != output.ExitInternal {
t.Fatalf("unsafe evidence exit = %d", output.ExitCodeOf(err))
}
}
func TestLedgerSelectorDoesNotCopySecrets(t *testing.T) {
c, _ := Lookup("im chat.members create")
s := NewSession(c)
s.ObserveRequest(map[string]any{
"id_list": []any{"ou_a"},
"content": "secret body",
"phone": "123",
"idempotency_key": "secret-key",
"access_token": "token",
"next_page_token": "page",
})
got, err := s.FinalizeSuccess(map[string]any{"invalid_id_list": []any{"ou_a"}})
if err != nil {
t.Fatal(err)
}
completion := got.Data.(map[string]any)["completion"].(Completion)
if len(completion.FailedItems) != 1 || completion.FailedItems[0] != "ou_a" {
t.Fatalf("completion leaked or lost selector: %#v", completion)
}
}
func TestFeedLedgerKeepsOnlyRetryableIdentityFields(t *testing.T) {
c, _ := Lookup("im feed.groups batch_add_item")
s := NewSession(c)
s.ObserveRequest(map[string]any{"items": []any{
map[string]any{"feed_id": "oc_a", "feed_type": "chat", "content": "secret"},
}})
got, err := s.FinalizeSuccess(map[string]any{"failed_items": []any{
map[string]any{"item": map[string]any{"feed_id": "oc_a", "feed_type": "chat"}, "error_message": "server text"},
}})
if err != nil {
t.Fatal(err)
}
item := got.Data.(map[string]any)["completion"].(Completion).FailedItems[0].(map[string]any)
if len(item) != 2 || item["feed_id"] != "oc_a" || item["feed_type"] != "chat" {
t.Fatalf("failed item = %#v", item)
}
}
func TestCompletionIsClosedOverRequestedItems(t *testing.T) {
simple := func(id string) ledgerItem { return ledgerItem{key: id, value: id} }
compound := func(feedType, feedID string) ledgerItem {
return ledgerItem{
key: feedType + "\x00" + feedID,
value: map[string]any{
"feed_id": feedID, "feed_type": feedType,
},
}
}
for _, tc := range []struct {
name string
requested []ledgerItem
failed []ledgerItem
pending []ledgerItem
}{
{
name: "single IDs",
requested: []ledgerItem{simple("a"), simple("b"), simple("c"), simple("a")},
failed: []ledgerItem{simple("b"), simple("c"), simple("c"), simple("unknown")},
pending: []ledgerItem{simple("b"), simple("b"), simple("pending-unknown")},
},
{
name: "compound IDs",
requested: []ledgerItem{
compound("chat", "oc_a"), compound("doc", "doc_b"), compound("chat", "oc_a"),
},
failed: []ledgerItem{
compound("chat", "oc_a"), compound("chat", "oc_a"), compound("chat", "oc_unknown"),
compound("doc", "doc_b"),
},
pending: []ledgerItem{
compound("doc", "doc_b"), compound("doc", "doc_b"), compound("doc", "doc_unknown"),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
got := completion(tc.requested, tc.failed, tc.pending, PartialRecoveryFailedItemsOnly)
if got.RequestedCount != got.SucceededCount+got.FailedCount+got.PendingCount {
t.Fatalf("non-exclusive counts: %#v", got)
}
if got.FailedCount != 1 || got.PendingCount != 1 {
t.Fatalf("failed/pending overlap was not resolved: %#v", got)
}
raw, err := json.Marshal(got)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(raw), "unknown") {
t.Fatalf("unrequested response item entered retry ledger: %s", raw)
}
})
}
}
func TestWriteSessionUnknownStrategyFailsClosed(t *testing.T) {
session := NewSession(Contract{
Key: "im future write",
Strategy: Strategy{Kind: StrategyKind("future_write")},
})
_, err := session.FinalizeSuccess(map[string]any{"accepted": true})
if err == nil || !errs.IsInternal(err) {
t.Fatalf("expected typed internal error, got %v", err)
}
}

View File

@@ -45,10 +45,13 @@ type EmitterConfig struct {
type EmitOptions struct {
Raw bool
Meta *Meta
Error interface{}
Hint string
Format string
JQ string
DryRun bool
Pretty PrettyRenderer
HintToStderr bool
JQSafetyWarning bool
}
@@ -101,18 +104,23 @@ func (e *Emitter) Success(data interface{}, opts EmitOptions) error {
return err
}
var err error
if opts.JQ != "" {
return e.emitEnvelope(data, true, opts)
err = e.emitEnvelope(data, true, opts)
} else {
switch opts.Format {
case "", "json":
err = e.emitEnvelope(data, true, opts)
case "pretty":
err = e.emitPretty(data, opts)
default:
err = e.emitFormatted(data, opts.Format)
}
}
switch opts.Format {
case "", "json":
return e.emitEnvelope(data, true, opts)
case "pretty":
return e.emitPretty(data, opts)
default:
return e.emitFormatted(data, opts.Format)
if err != nil {
return err
}
return e.emitHint(opts)
}
// PartialFailure emits a multi-status result whose envelope honestly reports
@@ -125,7 +133,10 @@ func (e *Emitter) PartialFailure(data interface{}, opts EmitOptions) error {
if err := e.requireOutput(); err != nil {
return err
}
return e.emitEnvelope(data, false, opts)
if err := e.emitEnvelope(data, false, opts); err != nil {
return err
}
return e.emitHint(opts)
}
// StreamPage scans and emits one page while retaining table/csv columns from
@@ -178,6 +189,25 @@ func (e *Emitter) StreamPage(data interface{}, opts StreamOptions) error {
})
}
// Hint writes recovery guidance to stderr through the same command-scoped
// output owner used for result emission.
func (e *Emitter) Hint(hint string) error {
return e.emitHint(EmitOptions{Hint: hint, HintToStderr: true})
}
// RedactedFallback atomically emits an already allowlisted fallback envelope.
// It deliberately skips safety scanning and jq: callers use it only after
// presentation failed, and must construct the envelope from fixed public
// fields rather than from the blocked payload.
func (e *Emitter) RedactedFallback(env Envelope) error {
if err := e.requireOutput(); err != nil {
return err
}
return e.emit(func(w io.Writer) error {
return WriteJSON(w, env)
})
}
func (e *Emitter) emitEnvelope(data interface{}, ok bool, opts EmitOptions) error {
scanResult := ScanForSafety(e.commandPath, data, e.errOut)
if scanResult.Blocked {
@@ -190,6 +220,8 @@ func (e *Emitter) emitEnvelope(data interface{}, ok bool, opts EmitOptions) erro
DryRun: opts.DryRun,
Data: data,
Meta: opts.Meta,
Error: opts.Error,
Hint: opts.Hint,
Notice: e.notice(),
}
if scanResult.Alert != nil {
@@ -316,6 +348,16 @@ func (e *Emitter) emit(render func(io.Writer) error) error {
return nil
}
func (e *Emitter) emitHint(opts EmitOptions) error {
if !opts.HintToStderr || opts.Hint == "" {
return nil
}
if _, err := fmt.Fprintf(e.errOut, "hint: %s\n", opts.Hint); err != nil {
return wrapOutputError("write", err)
}
return nil
}
func wrapOutputError(op string, err error) error {
return errs.NewInternalError(errs.SubtypeUnknown, "failed to %s command output", op).WithCause(err)
}

View File

@@ -63,6 +63,123 @@ func TestEmitterSuccessWritesAllBytes(t *testing.T) {
}
}
func TestEmitterPartialFailureCarriesContractFields(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "off")
stdout := &bytes.Buffer{}
complete := false
emitter := output.NewEmitter(output.EmitterConfig{
Out: stdout,
ErrOut: io.Discard,
CommandPath: "lark-cli im fixture",
Identity: "bot",
})
problem := errs.NewNetworkError(errs.SubtypeNetworkTransport, "request failed")
err := emitter.PartialFailure(
map[string]interface{}{"items": []interface{}{"kept"}},
output.EmitOptions{
Format: "json",
Meta: &output.Meta{
Complete: &complete,
PagesFetched: 1,
StopReason: "transport_error",
},
Error: problem,
Hint: "Retry the read.",
},
)
if err != nil {
t.Fatalf("Emitter.PartialFailure() error = %v", err)
}
var env output.Envelope
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
t.Fatalf("decode envelope: %v", err)
}
if env.OK || env.Hint != "Retry the read." || env.Meta == nil ||
env.Meta.Complete == nil || *env.Meta.Complete {
t.Fatalf("envelope = %#v, want typed incomplete result", env)
}
if env.Error == nil {
t.Fatalf("envelope = %#v, want structured error", env)
}
}
func TestEmitterJQProjectsContractHint(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "off")
stdout := &bytes.Buffer{}
emitter := output.NewEmitter(output.EmitterConfig{
Out: stdout,
ErrOut: io.Discard,
CommandPath: "lark-cli im fixture",
})
err := emitter.Success(map[string]interface{}{"id": "1"}, output.EmitOptions{
Format: "json",
JQ: ".hint",
Hint: "Use the same read entry point.",
})
if err != nil {
t.Fatalf("Emitter.Success() error = %v", err)
}
if got := strings.TrimSpace(stdout.String()); got != "Use the same read entry point." {
t.Fatalf("stdout = %q", got)
}
}
func TestEmitterNakedFormatWritesHintToStderr(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "off")
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
emitter := output.NewEmitter(output.EmitterConfig{
Out: stdout,
ErrOut: stderr,
CommandPath: "lark-cli im fixture",
})
err := emitter.Success([]interface{}{map[string]interface{}{"id": "1"}}, output.EmitOptions{
Format: "table",
Hint: "Result is incomplete.",
HintToStderr: true,
})
if err != nil {
t.Fatalf("Emitter.Success() error = %v", err)
}
if !strings.Contains(stderr.String(), "hint: Result is incomplete.") {
t.Fatalf("stderr = %q", stderr.String())
}
}
func TestEmitterRedactedFallbackSkipsBlockedPresentationScan(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "block")
extcs.Register(&contractSafetyProvider{alert: &extcs.Alert{
Provider: "emitter-contract",
MatchedRules: []string{"blocked-presentation"},
}})
t.Cleanup(func() { extcs.Register(nil) })
stdout := &bytes.Buffer{}
emitter := output.NewEmitter(output.EmitterConfig{
Out: stdout,
ErrOut: io.Discard,
CommandPath: "lark-cli im fixture",
})
err := emitter.RedactedFallback(output.Envelope{
OK: false,
Data: map[string]interface{}{"completion": map[string]interface{}{"status": "complete"}},
Error: errs.NewAPIError(errs.SubtypeUnknown, "Output failed after the IM write completed"),
})
if err != nil {
t.Fatalf("Emitter.RedactedFallback() error = %v", err)
}
var env output.Envelope
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
t.Fatalf("decode fallback: %v", err)
}
if env.OK || env.Error == nil {
t.Fatalf("fallback = %#v, want redacted failure envelope", env)
}
}
func TestEmitterMarshalFailureReturnsTypedErrorWithoutOutput(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "off")
stdout := &bytes.Buffer{}

View File

@@ -10,14 +10,20 @@ type Envelope struct {
DryRun bool `json:"dry_run,omitempty"`
Data interface{} `json:"data,omitempty"`
Meta *Meta `json:"meta,omitempty"`
Error interface{} `json:"error,omitempty"`
Hint string `json:"hint,omitempty"`
ContentSafetyAlert interface{} `json:"_content_safety_alert,omitempty"`
Notice map[string]interface{} `json:"_notice,omitempty"`
}
// Meta carries optional metadata in envelope responses.
type Meta struct {
Count int `json:"count,omitempty"`
Rollback string `json:"rollback,omitempty"`
Count int `json:"count,omitempty"`
Rollback string `json:"rollback,omitempty"`
Complete *bool `json:"complete,omitempty"`
PagesFetched int `json:"pages_fetched,omitempty"`
StopReason string `json:"stop_reason,omitempty"`
NextPageToken string `json:"next_page_token,omitempty"`
}
// PendingNotice, if set, returns system-level notices to inject as the

View File

@@ -48,3 +48,41 @@ func WriteSuccessEnvelope(data interface{}, opts SuccessEnvelopeOptions) error {
JQSafetyWarning: true,
})
}
// WriteEnvelope emits a complete result envelope. It is used when a result
// needs to carry business data and a machine-readable completion/error state
// in one stdout document.
func WriteEnvelope(env Envelope, opts SuccessEnvelopeOptions) error {
identity := env.Identity
if identity == "" {
identity = opts.Identity
}
noticeProvider := GetNotice
if env.Notice != nil {
notice := env.Notice
noticeProvider = func() map[string]interface{} {
return notice
}
}
emitter := NewEmitter(EmitterConfig{
Out: opts.Out,
ErrOut: opts.ErrOut,
CommandPath: opts.CommandPath,
Identity: identity,
NoticeProvider: noticeProvider,
})
emitOpts := EmitOptions{
Format: "",
Raw: false,
JQ: opts.JqExpr,
DryRun: env.DryRun || opts.DryRun,
Meta: env.Meta,
Error: env.Error,
Hint: env.Hint,
JQSafetyWarning: true,
}
if env.OK {
return emitter.Success(env.Data, emitOpts)
}
return emitter.PartialFailure(env.Data, emitOpts)
}

View File

@@ -212,3 +212,38 @@ func TestWriteSuccessEnvelope_BlockModeReturnsTypedErrorWithoutStdout(t *testing
t.Fatalf("stdout should stay empty on block, got: %s", out.String())
}
}
func TestEnvelopeCompleteSerializesFalse(t *testing.T) {
complete := false
raw, err := json.Marshal(Envelope{OK: true, Meta: &Meta{Complete: &complete}})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(raw), `"complete":false`) {
t.Fatalf("false completeness was omitted: %s", raw)
}
}
func TestWriteEnvelopeCarriesPartialResultAndTypedError(t *testing.T) {
var out strings.Builder
apiErr := errs.NewAPIError(errs.SubtypeUnknown, "one item failed")
err := WriteEnvelope(Envelope{
OK: false,
Data: map[string]any{"completion": map[string]any{"status": "partial"}},
Error: apiErr,
Hint: "retry only failed items",
}, SuccessEnvelopeOptions{Identity: "bot", Out: &out})
if err != nil {
t.Fatal(err)
}
var env map[string]any
if err := json.Unmarshal([]byte(out.String()), &env); err != nil {
t.Fatal(err)
}
if env["ok"] != false || env["hint"] != "retry only failed items" {
t.Fatalf("unexpected envelope: %#v", env)
}
if env["error"].(map[string]any)["type"] != "api" {
t.Fatalf("typed error missing: %#v", env)
}
}

View File

@@ -10,7 +10,9 @@ import (
"path/filepath"
"testing"
imcatalog "github.com/larksuite/cli/internal/imcontract/catalog"
"github.com/larksuite/cli/internal/qualitygate/manifest"
"github.com/larksuite/cli/internal/qualitygate/rules"
)
func TestManifestExportWritesManifestAndCommandIndex(t *testing.T) {
@@ -45,6 +47,16 @@ func TestManifestExportWritesManifestAndCommandIndex(t *testing.T) {
}
}
func TestExportedCommandIndexMatchesIMContractCatalog(t *testing.T) {
index, err := collectCommandIndex(context.Background())
if err != nil {
t.Fatalf("collectCommandIndex() error = %v", err)
}
if diags := rules.CheckIMContractCoverage(index, imcatalog.All()); len(diags) != 0 {
t.Fatalf("exported IM contract diagnostics = %#v", diags)
}
}
func TestManifestExportRequiresOutputPaths(t *testing.T) {
var stderr bytes.Buffer
code := runManifestExport(nil, &stderr)

View File

@@ -45,6 +45,18 @@ Adding a new row requires approval from the matching CODEOWNERS or quality gate
`legacy-commands.txt` only covers hand-authored legacy commands. Generated OpenAPI service commands are intentionally excluded from `command-manifest.json`; they are included in `command-index.json` only so command references can be checked against the real CLI surface.
## Public Domain Allowlists
`internal/qualitygate/config/allowlists/public-domains.txt` contains supported public hostnames approved for Go source. `fixture-domains.txt` contains test-only hostnames used by `*_test.go`, the repository-root `tests/` directory, or any `testdata/` directory; fixture entries do not apply to production Go files or `skills/`.
Keep one lowercase exact hostname per line, sorted alphabetically. Wildcards, suffix rules, duplicates, schemes, ports, and paths are rejected; approving `larkoffice.com` does not approve its subdomains.
RFC 2606 reserves the `.test`, `.example`, `.invalid`, and `.localhost` namespaces plus the exact names `example.com`, `example.net`, and `example.org`. These names are accepted without an allowlist entry and must not be listed.
Every public entry needs a current non-fixture Go use, evidence that it is a supported public endpoint, and CODEOWNER approval. Other test-only hostnames belong in the fixture list. Tenant-specific, private-control-plane, and internal API hostnames are not eligible.
`lint/domaincontract` validates both lists and scans complete Go files. In CI, unapproved-host findings are limited to values whose expressions intersect added lines; list validation and unused-entry checks remain repository-wide. See `lint/README.md` for scanner semantics.
## Semantic Blocker Policy
The semantic reviewer can propose findings, but the local gatekeeper recomputes whether each finding is reproducible from `facts.json`. A finding blocks only when all of these are true:

View File

@@ -0,0 +1,24 @@
# Exact test-only hostnames. Keep sorted.
abc.feishu.cn
attacker.example.com
bytedance.feishu.cn
cdn.feishu.cn
evil.example.com
example.feishu.cn
example.larkoffice.com
example.larksuite.com
feishu.cn
feishu.doubao.com
gateway.docker.internal
host.containers.internal
host.docker.internal
host.lima.internal
lf3-static.bytednsdoc.com
meetings.feishu.cn
meetings.larksuite.com
p3-lark-file.byteimg.com
passport.feishu.cn
sample.feishu.cn
x.feishu.cn
xxx.feishu.cn
xxx.larksuite.com

View File

@@ -0,0 +1,18 @@
# Exact public hostnames. Keep sorted.
accounts.feishu.cn
accounts.larksuite.com
applink.feishu.cn
applink.larksuite.com
ark.ap-southeast.bytepluses.com
github.com
larkoffice.com
lf-larkemail.bytetos.com
mcp.feishu.cn
mcp.larksuite.com
open.feishu.cn
open.larksuite.com
registry.npmjs.org
registry.npmmirror.com
sf16-sg.tiktokcdn.com
www.feishu.cn
www.larksuite.com

View File

@@ -0,0 +1,318 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package rules
import (
"fmt"
"sort"
"strings"
imcatalog "github.com/larksuite/cli/internal/imcontract/catalog"
"github.com/larksuite/cli/internal/qualitygate/manifest"
"github.com/larksuite/cli/internal/qualitygate/report"
)
const (
imContractCoverageRule = "im_contract_coverage"
expectedIMLeafCommands = 60
)
var acceptanceOnlyCommandAllowlist = map[imcatalog.ContractKey]struct{}{
"im chat.moderation update": {},
}
func CheckIMContractCoverage(commandIndex manifest.Manifest, contracts []imcatalog.Contract) []report.Diagnostic {
leafKeys := imLeafCommandKeys(commandIndex)
leafSet := make(map[string]struct{}, len(leafKeys))
for _, key := range leafKeys {
leafSet[key] = struct{}{}
}
contractSet := make(map[string]imcatalog.Contract, len(contracts))
for _, contract := range contracts {
contractSet[string(contract.Key)] = contract
}
commandByPath := make(map[string]manifest.Command, len(commandIndex.Commands))
for _, command := range commandIndex.Commands {
commandByPath[command.Path] = command
}
var diags []report.Diagnostic
for allowedKey := range acceptanceOnlyCommandAllowlist {
key := string(allowedKey)
if _, ok := leafSet[key]; !ok {
diags = append(diags, imContractDiagnostic(
key,
"acceptance_only allowlist key does not match a runnable IM leaf command",
))
}
contract, ok := contractSet[key]
if !ok {
diags = append(diags, imContractDiagnostic(
key,
"acceptance_only allowlist key has no completion contract",
))
} else if contract.Strategy.Kind != imcatalog.AcceptanceOnlyKind {
diags = append(diags, imContractDiagnostic(
key,
fmt.Sprintf("acceptance_only allowlist entry is stale for strategy kind %q", contract.Strategy.Kind),
))
}
}
if len(leafKeys) != expectedIMLeafCommands {
diags = append(diags, imContractDiagnostic(
"",
fmt.Sprintf("IM leaf command count is %d, want %d", len(leafKeys), expectedIMLeafCommands),
))
}
for _, key := range leafKeys {
if _, ok := contractSet[key]; !ok {
diags = append(diags, imContractDiagnostic(key, "IM leaf command has no completion contract"))
}
}
for _, contract := range contracts {
key := string(contract.Key)
if _, ok := leafSet[key]; !ok {
diags = append(diags, imContractDiagnostic(key, "IM contract key does not match a runnable leaf command"))
}
for _, message := range validateIMContractShape(contract, commandByPath[key]) {
diags = append(diags, imContractDiagnostic(key, message))
}
}
return diags
}
func validateIMContractShape(contract imcatalog.Contract, command manifest.Command) []string {
var messages []string
key := string(contract.Key)
if !strings.HasPrefix(key, "im ") {
messages = append(messages, "IM contract key must start with \"im \"")
}
kind := contract.Strategy.Kind
if !kind.IsRead() && !kind.IsWrite() {
return append(messages, fmt.Sprintf("IM contract has unknown strategy kind %q", kind))
}
if command.Path != "" {
switch {
case kind == imcatalog.MaterializeReadKind &&
command.Risk != "read" && command.Risk != "write":
messages = append(messages, fmt.Sprintf("IM materialize read contract requires command risk read or write, got %q", command.Risk))
case kind.IsRead() && kind != imcatalog.MaterializeReadKind && command.Risk != "read":
messages = append(messages, fmt.Sprintf("IM read contract requires command risk read, got %q", command.Risk))
case kind.IsWrite() && command.Risk != "write" && command.Risk != "high-risk-write":
messages = append(messages, fmt.Sprintf("IM write contract requires command risk write or high-risk-write, got %q", command.Risk))
}
}
switch kind {
case imcatalog.AcceptanceOnlyKind:
if contract.ReplayMode != imcatalog.ReplayForbidden {
messages = append(messages, fmt.Sprintf(
"acceptance_only requires replay mode %q, got %q",
imcatalog.ReplayForbidden,
contract.ReplayMode,
))
}
if _, ok := acceptanceOnlyCommandAllowlist[contract.Key]; !ok {
messages = append(messages, "acceptance_only is not allowed for this IM command")
}
case imcatalog.RequiredResultKind:
if message := validateRequiredSpec(contract.Strategy.Required); message != "" {
messages = append(messages, message)
}
case imcatalog.BatchPartialKind:
if contract.Strategy.ResultLedger == nil {
if message := validateEvidenceSpec("request", contract.Strategy.Request); message != "" {
messages = append(messages, message)
}
if len(contract.Strategy.Failures) == 0 && len(contract.Strategy.Pending) == 0 {
messages = append(messages, "batch_partial requires failures, pending evidence, or a result ledger")
}
messages = append(messages, validateEvidenceSpecs("failure", contract.Strategy.Failures)...)
messages = append(messages, validateEvidenceSpecs("pending", contract.Strategy.Pending)...)
} else {
if message := validateEvidenceSpec("result ledger", *contract.Strategy.ResultLedger); message != "" {
messages = append(messages, message)
}
if evidenceSpecPresent(contract.Strategy.Request) ||
len(contract.Strategy.Failures) > 0 || len(contract.Strategy.Pending) > 0 {
messages = append(messages, "batch_partial result ledger cannot be combined with request, failure, or pending evidence")
}
}
case imcatalog.RequiredResultBatchPartialKind:
if message := validateRequiredSpec(contract.Strategy.Required); message != "" {
messages = append(messages, message)
}
if message := validateEvidenceSpec("request", contract.Strategy.Request); message != "" {
messages = append(messages, message)
}
if len(contract.Strategy.Failures) == 0 {
messages = append(messages, "required_result_batch_partial requires failure evidence")
}
messages = append(messages, validateEvidenceSpecs("failure", contract.Strategy.Failures)...)
messages = append(messages, validateEvidenceSpecs("pending", contract.Strategy.Pending)...)
case imcatalog.ResponseSetAssertionKind:
if message := validateEvidenceSpec("request", contract.Strategy.Request); message != "" {
messages = append(messages, message)
}
if len(contract.Strategy.ResponseSets) == 0 {
messages = append(messages, "response_set_assertion requires response sets")
}
messages = append(messages, validateEvidenceSpecs("response set", contract.Strategy.ResponseSets)...)
if contract.Strategy.Assertion != imcatalog.AssertRequestedPresent &&
contract.Strategy.Assertion != imcatalog.AssertRequestedAbsent {
messages = append(messages, fmt.Sprintf("response_set_assertion has unknown assertion %q", contract.Strategy.Assertion))
}
case imcatalog.SearchReadKind:
if strings.TrimSpace(contract.Strategy.CollectionField) == "" {
messages = append(messages, "search_read requires collection field")
}
}
messages = append(messages, validateUnexpectedStrategyFields(contract.Strategy)...)
return messages
}
func validateUnexpectedStrategyFields(strategy imcatalog.Strategy) []string {
allowed := map[string]bool{"kind": true}
switch strategy.Kind {
case imcatalog.EntityReadKind:
allowed["read_hint"] = true
case imcatalog.SearchReadKind:
allowed["collection_field"] = true
allowed["requires_materialization"] = true
case imcatalog.RequiredResultKind:
allowed["required"] = true
case imcatalog.BatchPartialKind:
allowed["request"] = true
allowed["failures"] = true
allowed["pending"] = true
allowed["result_ledger"] = true
case imcatalog.RequiredResultBatchPartialKind:
allowed["required"] = true
allowed["request"] = true
allowed["failures"] = true
allowed["pending"] = true
case imcatalog.ResponseSetAssertionKind:
allowed["request"] = true
allowed["response_sets"] = true
allowed["assertion"] = true
}
present := map[string]bool{
"required": requiredSpecPresent(strategy.Required),
"request": evidenceSpecPresent(strategy.Request),
"failures": len(strategy.Failures) > 0,
"pending": len(strategy.Pending) > 0,
"response_sets": len(strategy.ResponseSets) > 0,
"assertion": strategy.Assertion != "",
"result_ledger": strategy.ResultLedger != nil,
"collection_field": strategy.CollectionField != "",
"requires_materialization": strategy.RequiresMaterialization,
"read_hint": strategy.ReadHint != "",
}
var messages []string
for field, isPresent := range present {
if isPresent && !allowed[field] {
messages = append(messages, fmt.Sprintf("%s must not set strategy field %s", strategy.Kind, field))
}
}
sort.Strings(messages)
return messages
}
func requiredSpecPresent(spec imcatalog.RequiredSpec) bool {
return spec.Shape != 0 || spec.Field != "" || spec.Child != ""
}
func validateEvidenceSpecs(label string, specs []imcatalog.EvidenceSpec) []string {
var messages []string
for index, spec := range specs {
indexedLabel := fmt.Sprintf("%s[%d]", label, index)
if message := validateEvidenceSpec(indexedLabel, spec); message != "" {
messages = append(messages, message)
}
}
return messages
}
func evidenceSpecPresent(spec imcatalog.EvidenceSpec) bool {
return spec.Shape != 0 || spec.Field != "" || spec.IDField != "" || spec.Container != ""
}
func validateRequiredSpec(spec imcatalog.RequiredSpec) string {
if strings.TrimSpace(spec.Field) == "" {
return "required_result requires a non-empty field"
}
switch spec.Shape {
case imcatalog.RequiredTopString, imcatalog.RequiredTopObject:
if spec.Child != "" {
return "top-level required_result must not set child"
}
case imcatalog.RequiredNestedString:
if strings.TrimSpace(spec.Child) == "" {
return "nested required_result requires a child field"
}
default:
return fmt.Sprintf("required_result has unknown shape %d", spec.Shape)
}
return ""
}
func validateEvidenceSpec(label string, spec imcatalog.EvidenceSpec) string {
if strings.TrimSpace(spec.Field) == "" {
return label + " evidence requires a non-empty field"
}
switch spec.Shape {
case imcatalog.EvidenceStrings, imcatalog.EvidenceFeedObjects:
case imcatalog.EvidenceObjects, imcatalog.EvidenceStatusObjects:
if strings.TrimSpace(spec.IDField) == "" {
return label + " evidence requires an ID field"
}
case imcatalog.EvidenceNestedObjects:
if strings.TrimSpace(spec.IDField) == "" || strings.TrimSpace(spec.Container) == "" {
return label + " nested evidence requires container and ID fields"
}
case imcatalog.EvidenceNestedFeedObjects:
if strings.TrimSpace(spec.Container) == "" {
return label + " nested feed evidence requires a container field"
}
default:
return fmt.Sprintf("%s evidence has unknown shape %d", label, spec.Shape)
}
return ""
}
func imLeafCommandKeys(commandIndex manifest.Manifest) []string {
var candidates []string
for _, cmd := range commandIndex.Commands {
if cmd.Domain == "im" && cmd.Runnable {
candidates = append(candidates, cmd.Path)
}
}
sort.Strings(candidates)
leaves := make([]string, 0, len(candidates))
for _, path := range candidates {
parent := false
for _, other := range candidates {
if other != path && strings.HasPrefix(other, path+" ") {
parent = true
break
}
}
if !parent {
leaves = append(leaves, path)
}
}
return leaves
}
func imContractDiagnostic(commandPath, message string) report.Diagnostic {
return report.Diagnostic{
Rule: imContractCoverageRule,
Action: report.ActionReject,
File: "command-index",
Message: message,
SubjectType: "command",
CommandPath: commandPath,
}
}

View File

@@ -0,0 +1,282 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package rules
import (
"fmt"
"strings"
"testing"
imcatalog "github.com/larksuite/cli/internal/imcontract/catalog"
qdiff "github.com/larksuite/cli/internal/qualitygate/diff"
"github.com/larksuite/cli/internal/qualitygate/manifest"
"github.com/larksuite/cli/internal/qualitygate/report"
)
func TestIMLeafCommandsExcludeParentsAndOtherDomains(t *testing.T) {
index := manifest.Manifest{Commands: []manifest.Command{
{Path: "im chat", Domain: "im", Runnable: true},
{Path: "im chat get", Domain: "im", Runnable: true},
{Path: "im chat list", Domain: "im", Runnable: false},
{Path: "docs chat get", Domain: "docs", Runnable: true},
}}
got := imLeafCommandKeys(index)
if len(got) != 1 || got[0] != "im chat get" {
t.Fatalf("IM leaves = %#v, want only runnable child", got)
}
}
func TestIMContractCoverageReportsMissingAndStaleKeys(t *testing.T) {
index, contracts := completeIMCoverageFixture()
contracts = contracts[1:]
contracts = append(contracts, imcatalog.Contract{
Key: "im stale command", Strategy: imcatalog.Strategy{Kind: imcatalog.EntityReadKind},
})
diags := CheckIMContractCoverage(index, contracts)
if !hasIMContractDiagnostic(diags, "im resource command00", "no completion contract") {
t.Fatalf("missing-command diagnostic absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, "im stale command", "does not match") {
t.Fatalf("stale-key diagnostic absent: %#v", diags)
}
}
func TestIMContractCoverageReportsMissingIMDomain(t *testing.T) {
index := manifest.Manifest{Commands: []manifest.Command{
{Path: "docs +fetch", Domain: "docs", Runnable: true},
}}
if leaves := imLeafCommandKeys(index); len(leaves) != 0 {
t.Fatalf("IM leaves = %#v, want none", leaves)
}
diags := CheckIMContractCoverage(index, imcatalog.All())
if !hasIMContractDiagnostic(diags, "", "IM leaf command count is 0, want 60") {
t.Fatalf("missing-domain diagnostic absent: %#v", diags)
}
}
func TestIMContractCoverageDiagnosticIsNotChangedFileFiltered(t *testing.T) {
diag := imContractDiagnostic("im +chat-list", "missing")
got := filterPRDiagnostics(
".",
"origin/main",
qdiff.FromChangedFiles([]string{"skills/lark-doc/SKILL.md"}),
manifest.Manifest{},
[]report.Diagnostic{diag},
)
if len(got) != 1 || got[0].Rule != imContractCoverageRule {
t.Fatalf("global IM coverage diagnostic was filtered: %#v", got)
}
}
func TestIMContractCoverageRejectsRiskAndStrategyShapeMismatches(t *testing.T) {
index, contracts := completeIMCoverageFixture()
index.Commands[0].Risk = "write"
contracts[1] = imcatalog.Contract{
Key: contracts[1].Key,
Strategy: imcatalog.Strategy{
Kind: imcatalog.RequiredResultKind,
Required: imcatalog.RequiredSpec{Shape: imcatalog.RequiredNestedString, Field: "message"},
},
ReplayMode: imcatalog.ReplayForbidden,
}
contracts[2] = imcatalog.Contract{
Key: contracts[2].Key,
Strategy: imcatalog.Strategy{
Kind: imcatalog.SearchReadKind,
CollectionField: "",
},
}
diags := CheckIMContractCoverage(index, contracts)
if !hasIMContractDiagnostic(diags, index.Commands[0].Path, "requires command risk read") {
t.Fatalf("read/write risk diagnostic absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, index.Commands[1].Path, "requires a child field") {
t.Fatalf("required shape diagnostic absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, index.Commands[2].Path, "requires collection field") {
t.Fatalf("search shape diagnostic absent: %#v", diags)
}
}
func TestIMContractCoverageAllowsMaterializeReadToWriteLocalOutput(t *testing.T) {
index, contracts := completeIMCoverageFixture()
index.Commands[0].Risk = "write"
contracts[0] = imcatalog.Contract{
Key: contracts[0].Key,
Strategy: imcatalog.Strategy{Kind: imcatalog.MaterializeReadKind},
}
diags := CheckIMContractCoverage(index, contracts)
for _, diagnostic := range diags {
if diagnostic.CommandPath == index.Commands[0].Path &&
strings.Contains(diagnostic.Message, "risk") {
t.Fatalf("materialize-read local write risk was rejected: %#v", diagnostic)
}
}
}
func TestIMContractCoverageRejectsUnknownKindAndNonIMKey(t *testing.T) {
index, contracts := completeIMCoverageFixture()
contracts[0] = imcatalog.Contract{
Key: "docs resource command00", Strategy: imcatalog.Strategy{Kind: imcatalog.StrategyKind("mystery")},
}
diags := CheckIMContractCoverage(index, contracts)
if !hasIMContractDiagnostic(diags, "docs resource command00", "must start with") ||
!hasIMContractDiagnostic(diags, "docs resource command00", "unknown strategy kind") {
t.Fatalf("unknown/non-IM diagnostics absent: %#v", diags)
}
}
func TestIMContractCoverageRestrictsAcceptanceOnlyContracts(t *testing.T) {
if len(acceptanceOnlyCommandAllowlist) != 1 {
t.Fatalf("acceptance-only allowlist = %#v, want only moderation update", acceptanceOnlyCommandAllowlist)
}
if _, ok := acceptanceOnlyCommandAllowlist["im chat.moderation update"]; !ok {
t.Fatalf("acceptance-only allowlist = %#v, want moderation update", acceptanceOnlyCommandAllowlist)
}
index, contracts := completeIMCoverageFixture()
if diags := CheckIMContractCoverage(index, contracts); len(diags) != 0 {
t.Fatalf("valid acceptance-only contract rejected: %#v", diags)
}
allowed := len(contracts) - 1
contracts[allowed].ReplayMode = imcatalog.ReplaySafe
if diags := CheckIMContractCoverage(index, contracts); !hasIMContractDiagnostic(
diags,
"im chat.moderation update",
"requires replay mode \"forbidden\"",
) {
t.Fatalf("replay-safe acceptance-only contract was not rejected: %#v", diags)
}
index, contracts = completeIMCoverageFixture()
index.Commands[0].Risk = "write"
contracts[0].Strategy = imcatalog.Strategy{Kind: imcatalog.AcceptanceOnlyKind}
contracts[0].ReplayMode = imcatalog.ReplayForbidden
if diags := CheckIMContractCoverage(index, contracts); !hasIMContractDiagnostic(
diags,
index.Commands[0].Path,
"is not allowed for this IM command",
) {
t.Fatalf("non-allowlisted acceptance-only contract was not rejected: %#v", diags)
}
index, contracts = completeIMCoverageFixture()
contracts[len(contracts)-1] = imcatalog.Contract{
Key: "im chat.moderation update",
Strategy: imcatalog.Strategy{
Kind: imcatalog.RequiredResultKind,
Required: imcatalog.RequiredSpec{Shape: imcatalog.RequiredNestedString, Field: "data"},
},
ReplayMode: imcatalog.ReplayForbidden,
}
if diags := CheckIMContractCoverage(index, contracts); !hasIMContractDiagnostic(
diags,
"im chat.moderation update",
"allowlist entry is stale",
) {
t.Fatalf("stale acceptance-only allowlist was not rejected: %#v", diags)
}
}
func TestIMContractCoverageRejectsIncompleteAndContradictoryEvidence(t *testing.T) {
index, contracts := completeIMCoverageFixture()
index.Commands[0].Risk = "write"
index.Commands[1].Risk = "write"
index.Commands[2].Risk = "write"
contracts[0] = imcatalog.Contract{
Key: contracts[0].Key,
Strategy: imcatalog.Strategy{
Kind: imcatalog.BatchPartialKind,
Request: imcatalog.EvidenceSpec{Shape: imcatalog.EvidenceStrings, Field: "ids"},
Failures: []imcatalog.EvidenceSpec{{Shape: imcatalog.EvidenceObjects, Field: "failed"}},
},
}
ledger := imcatalog.EvidenceSpec{Shape: imcatalog.EvidenceStatusObjects, Field: "results", IDField: "id"}
contracts[1] = imcatalog.Contract{
Key: contracts[1].Key,
Strategy: imcatalog.Strategy{
Kind: imcatalog.BatchPartialKind,
Request: imcatalog.EvidenceSpec{Shape: imcatalog.EvidenceStrings, Field: "ids"},
ResultLedger: &ledger,
},
}
contracts[2] = imcatalog.Contract{
Key: contracts[2].Key,
Strategy: imcatalog.Strategy{
Kind: imcatalog.ResponseSetAssertionKind,
Request: imcatalog.EvidenceSpec{Shape: imcatalog.EvidenceStrings, Field: "ids"},
ResponseSets: []imcatalog.EvidenceSpec{{Shape: imcatalog.EvidenceNestedObjects, Field: "members", IDField: "id"}},
Assertion: imcatalog.AssertRequestedPresent,
},
}
diags := CheckIMContractCoverage(index, contracts)
if !hasIMContractDiagnostic(diags, index.Commands[0].Path, "failure[0] evidence requires an ID field") {
t.Fatalf("failure shape diagnostic absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, index.Commands[1].Path, "result ledger cannot be combined") {
t.Fatalf("contradictory ledger diagnostic absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, index.Commands[2].Path, "response set[0] nested evidence requires container and ID fields") {
t.Fatalf("response-set shape diagnostic absent: %#v", diags)
}
}
func TestIMContractCoverageRejectsFieldsFromAnotherStrategyKind(t *testing.T) {
index, contracts := completeIMCoverageFixture()
contracts[0].Strategy.Required = imcatalog.RequiredSpec{
Shape: imcatalog.RequiredTopString,
Field: "message_id",
}
contracts[1].Strategy.ResponseSets = []imcatalog.EvidenceSpec{{
Shape: imcatalog.EvidenceStrings,
Field: "items",
}}
contracts[2].Strategy.CollectionField = "items"
diags := CheckIMContractCoverage(index, contracts)
if !hasIMContractDiagnostic(diags, index.Commands[0].Path, "entity_read must not set strategy field required") {
t.Fatalf("entity/required contradiction absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, index.Commands[1].Path, "entity_read must not set strategy field response_sets") {
t.Fatalf("entity/response-set contradiction absent: %#v", diags)
}
if !hasIMContractDiagnostic(diags, index.Commands[2].Path, "entity_read must not set strategy field collection_field") {
t.Fatalf("entity/search contradiction absent: %#v", diags)
}
}
func completeIMCoverageFixture() (manifest.Manifest, []imcatalog.Contract) {
index := manifest.Manifest{SchemaVersion: 1}
contracts := make([]imcatalog.Contract, 0, expectedIMLeafCommands)
for i := 0; i < expectedIMLeafCommands; i++ {
key := fmt.Sprintf("im resource command%02d", i)
risk := "read"
contract := imcatalog.Contract{
Key: imcatalog.ContractKey(key), Strategy: imcatalog.Strategy{Kind: imcatalog.EntityReadKind},
}
if i == expectedIMLeafCommands-1 {
key = "im chat.moderation update"
risk = "write"
contract = imcatalog.Contract{
Key: imcatalog.ContractKey(key),
Strategy: imcatalog.Strategy{Kind: imcatalog.AcceptanceOnlyKind},
ReplayMode: imcatalog.ReplayForbidden,
}
}
index.Commands = append(index.Commands, manifest.Command{Path: key, Domain: "im", Runnable: true, Risk: risk})
contracts = append(contracts, contract)
}
return index, contracts
}
func hasIMContractDiagnostic(diags []report.Diagnostic, key, text string) bool {
for _, diag := range diags {
if diag.CommandPath == key && strings.Contains(diag.Message, text) {
return true
}
}
return false
}

View File

@@ -11,6 +11,7 @@ import (
"sort"
"strings"
imcatalog "github.com/larksuite/cli/internal/imcontract/catalog"
qdiff "github.com/larksuite/cli/internal/qualitygate/diff"
manifestexamples "github.com/larksuite/cli/internal/qualitygate/examples"
"github.com/larksuite/cli/internal/qualitygate/facts"
@@ -43,6 +44,7 @@ func Run(ctx context.Context, opts Options) ([]report.Diagnostic, facts.Facts, e
if err := validateCommandIndexCoversManifest(m, commandIndex); err != nil {
return nil, facts.Facts{}, err
}
imContractDiags := CheckIMContractCoverage(commandIndex, imcatalog.All())
changed, err := qdiff.ChangedFiles(ctx, opts.Repo, opts.ChangedFrom)
if err != nil {
return nil, facts.Facts{}, err
@@ -110,6 +112,7 @@ func Run(ctx context.Context, opts Options) ([]report.Diagnostic, facts.Facts, e
}
diags = append(diags, publicContentDiagnostics(publicContent)...)
diags = filterPRDiagnostics(opts.Repo, opts.ChangedFrom, scope, m, diags)
diags = append(diags, imContractDiags...)
builtFacts := facts.BuildWithCommandLookup(m, commandIndex, skillFacts, skillQualityFacts, errorFacts, exampleFacts, outputFacts, diags, scope.Files)
return diags, facts.WithPublicContent(builtFacts, publicContentFacts(publicContent)), nil
@@ -212,6 +215,10 @@ func filterPRDiagnostics(repo, changedFrom string, scope qdiff.Scope, m manifest
commandScope := diagnosticCommandScopeFromFiles(scope.Files)
var out []report.Diagnostic
for _, diag := range diags {
if diag.Rule == imContractCoverageRule {
out = append(out, diag)
continue
}
if prDiagnosticRelevant(repo, scope.Files, commandScope, m, diag) {
out = append(out, diag)
}

View File

@@ -11,6 +11,7 @@ import (
"strings"
"testing"
imcatalog "github.com/larksuite/cli/internal/imcontract/catalog"
qdiff "github.com/larksuite/cli/internal/qualitygate/diff"
"github.com/larksuite/cli/internal/qualitygate/manifest"
"github.com/larksuite/cli/internal/qualitygate/report"
@@ -103,6 +104,55 @@ func TestRunRequiresCommandIndexToCoverManifest(t *testing.T) {
}
}
func TestRunReportsMissingIMDomain(t *testing.T) {
repo := t.TempDir()
runGit(t, repo, "init")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "config", "user.name", "Test User")
if err := vfs.WriteFile(filepath.Join(repo, "README.md"), []byte("# test\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit(t, repo, "add", "README.md")
runGit(t, repo, "commit", "-m", "base")
if err := vfs.MkdirAll(filepath.Join(repo, "skills"), 0o755); err != nil {
t.Fatal(err)
}
manifestPath := filepath.Join(repo, "command-manifest.json")
indexPath := filepath.Join(repo, "command-index.json")
m := manifest.Manifest{SchemaVersion: 1, Commands: []manifest.Command{{
Path: "docs +fetch", Domain: "docs", Source: manifest.SourceShortcut,
}}}
index := manifest.Manifest{SchemaVersion: 1, Commands: []manifest.Command{
{
Path: "docs +fetch", Domain: "docs", Source: manifest.SourceShortcut, Runnable: true,
},
{
Path: "drive files get", Domain: "drive", Source: manifest.SourceService, Generated: true, Runnable: true,
},
}}
if err := manifest.WriteFile(manifestPath, manifest.KindCommandManifest, m); err != nil {
t.Fatal(err)
}
if err := manifest.WriteFile(indexPath, manifest.KindCommandIndex, index); err != nil {
t.Fatal(err)
}
diags, _, err := Run(context.Background(), Options{
Repo: repo,
CLIBin: "./lark-cli",
ChangedFrom: "HEAD",
ManifestPath: manifestPath,
CommandIndexPath: indexPath,
})
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if !hasIMContractDiagnostic(diags, "", "IM leaf command count is 0, want 60") {
t.Fatalf("Run() missing-domain diagnostic absent: %#v", diags)
}
}
func TestRunReadsManifestFilesAndAcceptsServiceReferences(t *testing.T) {
repo := t.TempDir()
runGit(t, repo, "init")
@@ -160,6 +210,15 @@ description: Manage Drive comments with service command references.
},
},
}}
for _, contract := range imcatalog.All() {
risk := "read"
if contract.Strategy.Kind.IsWrite() {
risk = "write"
}
idx.Commands = append(idx.Commands, manifest.Command{
Path: string(contract.Key), Domain: "im", Source: manifest.SourceBuiltin, Runnable: true, Risk: risk,
})
}
if err := manifest.WriteFile(manifestPath, manifest.KindCommandManifest, m); err != nil {
t.Fatal(err)
}

View File

@@ -19,7 +19,7 @@ lint/
├── lintapi/ # shared types every domain returns
│ └── violation.go # Violation, Action, ActionReject / ActionLabel / ActionWarning
└── errscontract/ # first domain: typed-error contract guards
├── scan.go # ScanRepo(root) ([]lintapi.Violation, error) ← public entry
├── scan.go # ScanRepoWithOptions(root, opts) ← public entry
├── runner.go
├── typecheck.go
├── violation.go # local type aliases to lintapi
@@ -30,16 +30,19 @@ lint/
├── rule_subtype_classifier.go
├── rule_typed_error_completeness.go
└── *_test.go
└── domaincontract/ # endpoint domain contract: no hardcoded resolver hosts
├── scan.go # ScanRepo(root) ([]lintapi.Violation, error) ← public entry
── scan_test.go
└── domaincontract/ # resolver ownership + approved public hostname policy
├── scan.go # ScanRepoWithOptions(root, opts) ← public entry
── unapproved.go # Go AST/type-aware hostname extraction
├── policy.go # exact public/fixture allowlist validation
├── diff.go # added-line attribution
└── *_test.go
```
## Endpoint domain contract (`domaincontract`)
`domaincontract` is a syntax-level regression guard for the resolver-owned
Open, Accounts, MCP, and AppLink hosts used by the Go CLI. In production `.go`
files it rejects:
`domaincontract` contains two complementary Go source guards.
The resolver-ownership guard rejects:
- string literals containing a resolver-owned host FQDN
(`{open,accounts,mcp,applink}.{feishu.cn,larksuite.com}`), and
@@ -59,17 +62,54 @@ parse-level guard). The forbidden-host list is bound to the resolver source by
`TestForbiddenHostsMatchResolver`, so adding a resolver domain without updating
the guard fails the lint module's tests.
This is not a general outbound-URL or data-flow analyzer. It does not inspect
non-Go assets, hosts assembled from string fragments, SDK constructor option
flow, or previously unknown Feishu/Lark hosts. The literal rule and code review
remain the backstop for those cases.
The approved-domain guard parses every Git-tracked Go file in full. In CI,
unapproved-host findings are limited to values whose expressions intersect an
added line; policy validation and unused-entry checks remain repository-wide.
It rejects an exact hostname unless it is present in one of:
To add or change an outbound endpoint, edit the resolver — never hardcode a host.
- `internal/qualitygate/config/allowlists/public-domains.txt`, for production
and test code; or
- `internal/qualitygate/config/allowlists/fixture-domains.txt`, only for
`*_test.go`, the repository-root `tests/`, and any `testdata/` (never
`skills/`).
RFC 2606 example/test names are accepted independently of those lists. This
includes the reserved `.test`, `.example`, `.invalid`, and `.localhost`
namespaces and the exact names `example.com`, `example.net`, and `example.org`;
they are safe placeholders rather than supported public endpoints.
High-confidence evidence is deliberately limited to static string expressions
assigned to `host`, `hostname`, or `domain` semantic names (including common
case/plural forms and collections), plus static strings whose entire value is
an absolute `http`, `https`, `ws`, or `wss` URL. It supports Go literals,
escapes, compile-time concatenation, constant references, grouped declarations,
multi-value assignments, and multiline expressions. Bare domain-shaped strings
without hostname semantics are not blocked.
Sequence values are scanned individually. For a hostname-semantic map, a key or
value is evidence only when it is the sole hostname-shaped side of that entry;
ambiguous string-to-string entries are not guessed. Struct fields use Go type
information so known non-network `Host` / `Domain` fields do not become hostname
evidence merely because an enum or command category contains a dot.
Allowlist matching is lowercase and exact: there are no wildcard, suffix, DNS,
or public-suffix exceptions. Entries must be sorted and unique, use ASCII
hostnames, and have a current in-scope use. See
`internal/qualitygate/config/README.md` for admission and approval rules.
This is not a general outbound-URL or cross-language data-flow analyzer. It does
not inspect non-Go assets or dynamically constructed values.
To add or change a resolver-owned Feishu/Lark endpoint, edit the resolver rather
than hardcoding the host elsewhere.
## Running
```bash
# from the repo root (one level above lint/)
# PR-scoped scan from the repo root (one level above lint/)
go run -C lint . --changed-from <base-revision> ..
# Full inventory (also reports historical unapproved hostnames)
go run -C lint . ..
```
@@ -100,10 +140,14 @@ Exit codes follow `lint/main.go`:
import "github.com/larksuite/cli/lint/lintapi"
// ScanRepo walks root and returns every violation produced by this
// domain's checks. Domains MUST return []lintapi.Violation so the
// top-level dispatcher can aggregate uniformly.
func ScanRepo(root string) ([]lintapi.Violation, error) { ... }
type ScanOptions struct {
ChangedFrom string
}
// ScanRepoWithOptions walks root and returns every violation produced
// by this domain's checks. Domains MUST return []lintapi.Violation so
// the top-level dispatcher can aggregate uniformly.
func ScanRepoWithOptions(root string, opts ScanOptions) ([]lintapi.Violation, error) { ... }
```
3. Per-rule files are named `rule_<name>.go` with sibling
@@ -114,8 +158,12 @@ Exit codes follow `lint/main.go`:
```go
var scanners = []scanner{
{name: "errscontract", fn: errscontract.ScanRepo},
{name: "<domain>", fn: <domain>.ScanRepo}, // ← add here
{name: "errscontract", fn: errscontract.ScanRepoWithOptions},
{name: "<domain>", fn: func(root string, opts errscontract.ScanOptions) ([]lintapi.Violation, error) {
return <domain>.ScanRepoWithOptions(root, <domain>.ScanOptions{
ChangedFrom: opts.ChangedFrom,
})
}},
}
```

171
lint/domaincontract/diff.go Normal file
View File

@@ -0,0 +1,171 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import (
"bytes"
"fmt"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
type addedLineRange struct {
Start int
End int
}
type changedGoPath struct {
Old string
New string
}
var unifiedHunkRE = regexp.MustCompile(`^@@ -[0-9]+(?:,[0-9]+)? \+([0-9]+)(?:,([0-9]+))? @@`)
func changedGoLineRanges(root, from string) (map[string][]addedLineRange, error) {
if from == "" {
return nil, nil
}
names, err := gitCommandOutput(
root,
"diff",
"--name-status",
"-z",
"--find-renames",
"--diff-filter=ACMR",
from+"...HEAD",
"--",
)
if err != nil {
return nil, fmt.Errorf("list changed Go files: %w", err)
}
paths, err := parseChangedGoPaths(names)
if err != nil {
return nil, fmt.Errorf("parse changed Go files: %w", err)
}
out := map[string][]addedLineRange{}
for _, path := range paths {
args := []string{
"diff",
"--unified=0",
"--no-color",
"--no-ext-diff",
"--find-renames",
"--diff-filter=ACMR",
from + "...HEAD",
"--",
}
if path.Old != path.New {
args = append(args, path.Old)
}
args = append(args, path.New)
patch, err := gitCommandOutput(root, args...)
if err != nil {
return nil, fmt.Errorf("read diff for %s: %w", path.New, err)
}
ranges, err := parseAddedLineRanges(patch)
if err != nil {
return nil, fmt.Errorf("parse diff for %s: %w", path.New, err)
}
out[path.New] = ranges
}
return out, nil
}
func parseChangedGoPaths(raw []byte) ([]changedGoPath, error) {
fields := bytes.Split(raw, []byte{0})
var out []changedGoPath
for i := 0; i < len(fields); {
status := string(fields[i])
i++
if status == "" {
break
}
if i >= len(fields) || len(fields[i]) == 0 {
return nil, fmt.Errorf("truncated name-status record")
}
oldPath := filepath.ToSlash(string(fields[i]))
i++
newPath := oldPath
if status[0] == 'R' || status[0] == 'C' {
if i >= len(fields) || len(fields[i]) == 0 {
return nil, fmt.Errorf("truncated rename/copy record for %q", oldPath)
}
newPath = filepath.ToSlash(string(fields[i]))
i++
if status[0] == 'C' {
// A copy introduces every destination line. Diff only the new
// path so Git presents it as an added file rather than a
// metadata-only copy with no added-line ranges.
oldPath = newPath
}
}
if !strings.HasSuffix(newPath, ".go") {
continue
}
out = append(out, changedGoPath{Old: oldPath, New: newPath})
}
return out, nil
}
func parseAddedLineRanges(patch []byte) ([]addedLineRange, error) {
var out []addedLineRange
for _, raw := range bytes.Split(patch, []byte{'\n'}) {
line := string(raw)
if !strings.HasPrefix(line, "@@") {
continue
}
match := unifiedHunkRE.FindStringSubmatch(line)
if match == nil {
return nil, fmt.Errorf("unsupported unified hunk header %q", line)
}
start, err := strconv.Atoi(match[1])
if err != nil {
return nil, fmt.Errorf("parse added start line in %q: %w", line, err)
}
count := 1
if match[2] != "" {
count, err = strconv.Atoi(match[2])
if err != nil {
return nil, fmt.Errorf("parse added line count in %q: %w", line, err)
}
}
if count == 0 {
continue
}
out = append(out, addedLineRange{Start: start, End: start + count - 1})
}
return out, nil
}
func firstAddedLineInSpan(ranges []addedLineRange, start, end int) (int, bool) {
for _, r := range ranges {
if start <= r.End && end >= r.Start {
if start > r.Start {
return start, true
}
return r.Start, true
}
}
return 0, false
}
func gitCommandOutput(root string, args ...string) ([]byte, error) {
cmd := exec.Command("git", args...)
cmd.Dir = root
out, err := cmd.Output()
if err == nil {
return out, nil
}
if exitErr, ok := err.(*exec.ExitError); ok {
stderr := strings.TrimSpace(string(exitErr.Stderr))
if stderr != "" {
return nil, fmt.Errorf("%w: %s", err, stderr)
}
}
return nil, err
}

View File

@@ -0,0 +1,96 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import "testing"
func TestParseChangedGoPaths(t *testing.T) {
raw := []byte("M\x00changed.go\x00R100\x00old.go\x00renamed.go\x00C100\x00source.go\x00copied.go\x00A\x00README.md\x00")
got, err := parseChangedGoPaths(raw)
if err != nil {
t.Fatal(err)
}
want := []changedGoPath{
{Old: "changed.go", New: "changed.go"},
{Old: "old.go", New: "renamed.go"},
{Old: "copied.go", New: "copied.go"},
}
if len(got) != len(want) {
t.Fatalf("paths = %#v, want %#v", got, want)
}
for i := range got {
if got[i] != want[i] {
t.Fatalf("paths = %#v, want %#v", got, want)
}
}
}
func TestParseChangedGoPathsRejectsTruncatedRename(t *testing.T) {
if _, err := parseChangedGoPaths([]byte("R100\x00old.go\x00")); err == nil {
t.Fatal("expected truncated rename error")
}
}
func TestParseAddedLineRanges(t *testing.T) {
patch := []byte(`diff --git a/x.go b/x.go
index 1111111..2222222 100644
--- a/x.go
+++ b/x.go
@@ -2,0 +3,2 @@
+first
+second
@@ -10 +12 @@
-old
+new
@@ -20 +21,0 @@
-deleted
`)
got, err := parseAddedLineRanges(patch)
if err != nil {
t.Fatal(err)
}
want := []addedLineRange{{Start: 3, End: 4}, {Start: 12, End: 12}}
if len(got) != len(want) {
t.Fatalf("ranges = %#v, want %#v", got, want)
}
for i := range got {
if got[i] != want[i] {
t.Fatalf("ranges = %#v, want %#v", got, want)
}
}
}
func TestParseAddedLineRangesRejectsUnknownHunk(t *testing.T) {
if _, err := parseAddedLineRanges([]byte("@@@ unsupported @@@\n")); err == nil {
t.Fatal("expected unsupported hunk error")
}
}
func TestFirstAddedLineInSpan(t *testing.T) {
ranges := []addedLineRange{{Start: 5, End: 7}, {Start: 10, End: 10}}
tests := []struct {
start, end int
line int
ok bool
}{
{start: 1, end: 4, ok: false},
{start: 4, end: 6, line: 5, ok: true},
{start: 6, end: 9, line: 6, ok: true},
{start: 8, end: 12, line: 10, ok: true},
}
for _, tc := range tests {
line, ok := firstAddedLineInSpan(ranges, tc.start, tc.end)
if line != tc.line || ok != tc.ok {
t.Errorf(
"firstAddedLineInSpan(%d, %d) = (%d, %v), want (%d, %v)",
tc.start,
tc.end,
line,
ok,
tc.line,
tc.ok,
)
}
}
}

View File

@@ -0,0 +1,126 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
publicDomainsPath = "internal/qualitygate/config/allowlists/public-domains.txt"
fixtureDomainsPath = "internal/qualitygate/config/allowlists/fixture-domains.txt"
)
type domainPolicyEntry struct {
Host string
File string
Line int
}
type domainPolicy struct {
Public map[string]domainPolicyEntry
Fixtures map[string]domainPolicyEntry
}
// isReservedExampleHostname recognizes only names reserved by RFC 2606 for
// examples, testing, invalid-name examples, and localhost use. These names are
// safe source placeholders and are policy exceptions, not supported public
// endpoints.
func isReservedExampleHostname(host string) bool {
host = strings.TrimSuffix(strings.ToLower(strings.TrimSpace(host)), ".")
switch host {
case "example.com", "example.net", "example.org":
return true
}
labels := strings.Split(host, ".")
switch labels[len(labels)-1] {
case "test", "example", "invalid", "localhost":
return true
default:
return false
}
}
func loadDomainPolicy(root string) (domainPolicy, error) {
public, err := loadDomainList(root, publicDomainsPath)
if err != nil {
return domainPolicy{}, err
}
fixtures, err := loadDomainList(root, fixtureDomainsPath)
if err != nil {
return domainPolicy{}, err
}
for host, entry := range fixtures {
if publicEntry, ok := public[host]; ok {
return domainPolicy{}, fmt.Errorf(
"%s:%d: hostname %q is already listed at %s:%d",
entry.File, entry.Line, host, publicEntry.File, publicEntry.Line,
)
}
}
return domainPolicy{Public: public, Fixtures: fixtures}, nil
}
func loadDomainList(root, rel string) (map[string]domainPolicyEntry, error) {
path := filepath.Join(root, filepath.FromSlash(rel))
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open domain allowlist %s: %w", rel, err)
}
defer file.Close()
entries := map[string]domainPolicyEntry{}
var previous string
scanner := bufio.NewScanner(file)
for line := 1; scanner.Scan(); line++ {
host := strings.TrimSpace(scanner.Text())
if host == "" || strings.HasPrefix(host, "#") {
continue
}
if host != strings.ToLower(host) {
return nil, fmt.Errorf("%s:%d: hostname must be lowercase: %q", rel, line, host)
}
if err := validatePolicyHostname(host); err != nil {
return nil, fmt.Errorf("%s:%d: %w", rel, line, err)
}
if previous != "" && host <= previous {
return nil, fmt.Errorf("%s:%d: hostnames must be unique and sorted: %q", rel, line, host)
}
entries[host] = domainPolicyEntry{Host: host, File: rel, Line: line}
previous = host
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("read domain allowlist %s: %w", rel, err)
}
if len(entries) == 0 {
return nil, fmt.Errorf("%s: domain list must not be empty", rel)
}
return entries, nil
}
func validatePolicyHostname(host string) error {
if len(host) > 253 || !strings.Contains(host, ".") || strings.HasSuffix(host, ".") {
return fmt.Errorf("invalid exact hostname %q", host)
}
labels := strings.Split(host, ".")
for _, label := range labels {
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return fmt.Errorf("invalid exact hostname %q", host)
}
for _, r := range label {
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' {
continue
}
return fmt.Errorf("invalid exact hostname %q", host)
}
}
if !strings.ContainsAny(labels[len(labels)-1], "abcdefghijklmnopqrstuvwxyz") {
return fmt.Errorf("invalid exact hostname %q", host)
}
return nil
}

View File

@@ -0,0 +1,120 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import (
"strings"
"testing"
)
func TestLoadDomainPolicy(t *testing.T) {
root := t.TempDir()
writeFile(t, root, publicDomainsPath, "# public\napi.example.com\nwww.example.com\n")
writeFile(t, root, fixtureDomainsPath, "# fixtures\nfixture.example.com\n")
policy, err := loadDomainPolicy(root)
if err != nil {
t.Fatal(err)
}
if len(policy.Public) != 2 || len(policy.Fixtures) != 1 {
t.Fatalf("unexpected policy sizes: public=%d fixtures=%d", len(policy.Public), len(policy.Fixtures))
}
if policy.Public["api.example.com"].Line != 2 {
t.Fatalf("api.example.com line = %d, want 2", policy.Public["api.example.com"].Line)
}
}
func TestLoadDomainPolicyRejectsInvalidLists(t *testing.T) {
tests := []struct {
name string
public string
fixtures string
want string
}{
{
name: "uppercase",
public: "API.example.com\n",
fixtures: "fixture.example.com\n",
want: "must be lowercase",
},
{
name: "unsorted",
public: "www.example.com\napi.example.com\n",
fixtures: "fixture.example.com\n",
want: "unique and sorted",
},
{
name: "duplicate",
public: "api.example.com\napi.example.com\n",
fixtures: "fixture.example.com\n",
want: "unique and sorted",
},
{
name: "wildcard",
public: "*.example.com\n",
fixtures: "fixture.example.com\n",
want: "invalid exact hostname",
},
{
name: "scheme",
public: "https://example.com\n",
fixtures: "fixture.example.com\n",
want: "invalid exact hostname",
},
{
name: "path",
public: "api.example.com/v1\n",
fixtures: "fixture.example.com\n",
want: "invalid exact hostname",
},
{
name: "port",
public: "api.example.com:443\n",
fixtures: "fixture.example.com\n",
want: "invalid exact hostname",
},
{
name: "cross-list duplicate",
public: "api.example.com\n",
fixtures: "api.example.com\n",
want: "already listed",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
root := t.TempDir()
writeFile(t, root, publicDomainsPath, tc.public)
writeFile(t, root, fixtureDomainsPath, tc.fixtures)
_, err := loadDomainPolicy(root)
if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatalf("loadDomainPolicy() error = %v, want substring %q", err, tc.want)
}
})
}
}
func TestReservedExampleHostname(t *testing.T) {
for _, host := range []string{
"example.com",
"example.net",
"example.org",
"example.test",
"docs.example",
"missing.invalid",
"service.localhost",
} {
if !isReservedExampleHostname(host) {
t.Errorf("%q should be a reserved example hostname", host)
}
}
for _, host := range []string{
"attacker.example.com",
"example.dev",
"private.corp.internal",
} {
if isReservedExampleHostname(host) {
t.Errorf("%q must still require policy approval", host)
}
}
}

View File

@@ -1,8 +1,8 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
// Package domaincontract guards the Go CLI against direct reuse of the current
// resolver-owned host FQDNs outside core.ResolveEndpoints.
// Package domaincontract guards resolver ownership and rejects newly introduced
// static Go hostnames that are not covered by the repository domain policy.
package domaincontract
import (
@@ -11,6 +11,7 @@ import (
"go/token"
"io/fs"
"path/filepath"
"sort"
"strconv"
"strings"
@@ -75,10 +76,40 @@ func skipDir(name string) bool {
return false
}
// ScanRepo walks production .go files under root and flags string literals
// containing a forbidden resolver host outside the allowlist. Comments and
// _test.go files are not scanned.
// ScanRepo runs the resolver-owned endpoint guard and a full repository domain
// inventory. CI should use ScanRepoWithOptions with a changed-from revision so
// historical unapproved domains are not attributed to an unrelated change.
func ScanRepo(root string) ([]lintapi.Violation, error) {
return ScanRepoWithOptions(root, ScanOptions{})
}
type ScanOptions struct {
ChangedFrom string
}
func ScanRepoWithOptions(root string, opts ScanOptions) ([]lintapi.Violation, error) {
out, err := scanHardcodedEndpoints(root)
if err != nil {
return nil, err
}
domainViolations, err := scanUnapprovedDomains(root, opts)
if err != nil {
return nil, err
}
out = append(out, domainViolations...)
sort.SliceStable(out, func(i, j int) bool {
if out[i].File != out[j].File {
return out[i].File < out[j].File
}
if out[i].Line != out[j].Line {
return out[i].Line < out[j].Line
}
return out[i].Rule < out[j].Rule
})
return out, nil
}
func scanHardcodedEndpoints(root string) ([]lintapi.Violation, error) {
var out []lintapi.Violation
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {

View File

@@ -0,0 +1,911 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import (
"fmt"
"go/ast"
"go/constant"
"go/parser"
"go/token"
"go/types"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
"github.com/larksuite/cli/lint/lintapi"
"golang.org/x/tools/go/packages"
)
const (
unapprovedDomainRule = "unapproved-domain"
unusedDomainRule = "domain-allowlist-unused"
incompleteDomainRule = "domain-scan-incomplete"
)
type typedGoFile struct {
File *ast.File
Fset *token.FileSet
Info *types.Info
}
type domainEvidence struct {
Host string
Kind string
Expr ast.Expr
}
type evidenceKey struct {
Host string
Start, End token.Pos
}
type fileDomainScan struct {
File *ast.File
Fset *token.FileSet
Info *types.Info
Evidence []domainEvidence
TypeInfoRequired []ast.Expr
seen map[evidenceKey]bool
parents map[ast.Node]ast.Node
}
type collectionCompositeKind uint8
const (
notCollectionComposite collectionCompositeKind = iota
sequenceComposite
mapComposite
)
type hostnameFieldID struct {
Type string
Field string
}
var nonNetworkHostnameFields = map[hostnameFieldID]bool{
{Type: "github.com/larksuite/cli/events/im.CardActionTriggerOutput", Field: "Host"}: true,
{Type: "github.com/larksuite/cli/internal/cmdmeta.Meta", Field: "Domain"}: true,
}
func scanUnapprovedDomains(root string, opts ScanOptions) ([]lintapi.Violation, error) {
root, err := filepath.Abs(root)
if err != nil {
return nil, fmt.Errorf("resolve repository root: %w", err)
}
publicPath := filepath.Join(root, filepath.FromSlash(publicDomainsPath))
if _, err := os.Stat(publicPath); err != nil {
if os.IsNotExist(err) {
if _, goModErr := os.Stat(filepath.Join(root, "go.mod")); os.IsNotExist(goModErr) {
return nil, nil
}
}
return nil, fmt.Errorf("domain policy unavailable: %w", err)
}
policy, err := loadDomainPolicy(root)
if err != nil {
return nil, err
}
added, err := changedGoLineRanges(root, opts.ChangedFrom)
if err != nil {
return nil, err
}
typed, typeLoadErr := loadTypedGoFiles(root)
goFiles, err := trackedGoFiles(root)
if err != nil {
return nil, err
}
observedPublic := map[string]bool{}
observedFixtures := map[string]bool{}
inventoryComplete := typeLoadErr == nil
var out []lintapi.Violation
parseFailureReported := false
typeInfoGapReported := false
for _, rel := range goFiles {
path := filepath.Join(root, filepath.FromSlash(rel))
parsedFset := token.NewFileSet()
parsedFile, parseErr := parser.ParseFile(parsedFset, path, nil, 0)
if parseErr != nil {
inventoryComplete = false
if opts.ChangedFrom == "" {
out = append(out, incompleteDomainViolation(rel, parseErr))
parseFailureReported = true
} else if _, changed := added[rel]; changed {
out = append(out, incompleteDomainViolation(rel, parseErr))
parseFailureReported = true
}
continue
}
tf, ok := typed[filepath.Clean(path)]
if !ok {
tf = typedGoFile{File: parsedFile, Fset: parsedFset}
}
scan := newFileDomainScan(tf)
scan.collectSemanticEvidence()
scan.collectAbsoluteURLEvidence()
if len(scan.TypeInfoRequired) > 0 {
// Inventory completeness is a property of the whole HEAD. Whether
// this PR owns an incomplete-scan diagnostic is decided separately
// by the added-line intersection below.
inventoryComplete = false
}
for _, expr := range scan.TypeInfoRequired {
start := tf.Fset.Position(expr.Pos()).Line
end := tf.Fset.Position(expr.End()).Line
line := start
if opts.ChangedFrom != "" {
var intersects bool
line, intersects = firstAddedLineInSpan(added[rel], start, end)
if !intersects {
continue
}
}
typeInfoGapReported = true
out = append(out, incompleteDomainViolationAt(
rel,
line,
fmt.Errorf("Go type information unavailable for hostname-oriented field evidence"),
))
break
}
fixture := isDomainFixturePath(rel)
// The detector's own policy literals and contract corpus may be
// scanned, but they cannot justify keeping an allowlist entry.
policyOwner := strings.HasPrefix(rel, "lint/domaincontract/")
for _, evidence := range scan.Evidence {
if isReservedExampleHostname(evidence.Host) {
continue
}
if _, ok := policy.Public[evidence.Host]; ok {
if !fixture && !policyOwner {
observedPublic[evidence.Host] = true
}
continue
}
if _, ok := policy.Fixtures[evidence.Host]; ok && fixture {
if !policyOwner {
observedFixtures[evidence.Host] = true
}
continue
}
start := tf.Fset.Position(evidence.Expr.Pos()).Line
end := tf.Fset.Position(evidence.Expr.End()).Line
line := start
if opts.ChangedFrom != "" {
var intersects bool
line, intersects = firstAddedLineInSpan(added[rel], start, end)
if !intersects {
continue
}
}
suggestion := "remove the hostname or replace it with an approved public endpoint; " +
"public allowlist additions require evidence and CODEOWNER approval"
if _, fixtureOnly := policy.Fixtures[evidence.Host]; fixtureOnly && !fixture {
suggestion = "remove the fixture-only hostname or move this use into an approved fixture scope; " +
"fixture entries are not approved for production Go code or skills"
}
out = append(out, lintapi.Violation{
Rule: unapprovedDomainRule,
Action: lintapi.ActionReject,
File: rel,
Line: line,
Message: fmt.Sprintf(
"unapproved hostname %q found in %s",
evidence.Host,
evidence.Kind,
),
Suggestion: suggestion,
})
}
}
// A syntax error is also surfaced by go/packages. Prefer the file-specific
// parse diagnostic when one was already reported; otherwise make a
// repository-wide type-loading failure explicit instead of silently
// continuing without the type information required by field evidence.
if typeLoadErr != nil && !parseFailureReported && !typeInfoGapReported {
out = append(out, incompleteDomainViolation("go.mod", typeLoadErr))
}
if inventoryComplete {
for host, entry := range policy.Public {
if !observedPublic[host] {
out = append(out, unusedDomainViolation(entry))
}
}
for host, entry := range policy.Fixtures {
if !observedFixtures[host] {
out = append(out, unusedDomainViolation(entry))
}
}
}
return out, nil
}
func trackedGoFiles(root string) ([]string, error) {
out, err := gitCommandOutput(root, "ls-files", "-z", "--", "*.go")
if err != nil {
return nil, fmt.Errorf("list tracked Go files: %w", err)
}
var files []string
for _, raw := range strings.Split(string(out), "\x00") {
if raw == "" {
continue
}
rel := filepath.ToSlash(raw)
if strings.HasPrefix(rel, "vendor/") || strings.HasPrefix(rel, "node_modules/") {
continue
}
files = append(files, rel)
}
return files, nil
}
func loadTypedGoFiles(root string) (map[string]typedGoFile, error) {
moduleDirs, err := trackedGoModuleDirs(root)
if err != nil {
return nil, err
}
out := map[string]typedGoFile{}
var firstLoadErr error
var loadErrCount int
for _, moduleDir := range moduleDirs {
moduleRoot := root
if moduleDir != "." {
moduleRoot = filepath.Join(root, filepath.FromSlash(moduleDir))
}
files, err := loadTypedGoModule(moduleRoot)
for path, file := range files {
out[path] = file
}
if err != nil {
loadErrCount++
if firstLoadErr == nil {
firstLoadErr = err
}
}
}
if loadErrCount == 1 {
return out, firstLoadErr
}
if loadErrCount > 1 {
return out, fmt.Errorf("%w (and %d more module errors)", firstLoadErr, loadErrCount-1)
}
return out, nil
}
func trackedGoModuleDirs(root string) ([]string, error) {
raw, err := gitCommandOutput(root, "ls-files", "-z")
if err != nil {
return nil, fmt.Errorf("list tracked Go modules: %w", err)
}
var dirs []string
for _, path := range strings.Split(string(raw), "\x00") {
path = filepath.ToSlash(path)
if path != "go.mod" && !strings.HasSuffix(path, "/go.mod") {
continue
}
dir := filepath.ToSlash(filepath.Dir(path))
dirs = append(dirs, dir)
}
return dirs, nil
}
func loadTypedGoModule(moduleRoot string) (map[string]typedGoFile, error) {
fset := token.NewFileSet()
cfg := &packages.Config{
Mode: packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles |
packages.NeedImports |
packages.NeedDeps |
packages.NeedTypes |
packages.NeedSyntax |
packages.NeedTypesInfo,
Dir: moduleRoot,
Fset: fset,
Tests: true,
}
pkgs, err := packages.Load(cfg, "./...")
if err != nil {
return nil, fmt.Errorf("load Go type information: %w", err)
}
out := map[string]typedGoFile{}
var firstPackageErr string
var packageErrCount int
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
if pkg == nil {
return
}
for _, pkgErr := range pkg.Errors {
packageErrCount++
if firstPackageErr == "" {
firstPackageErr = pkgErr.Error()
}
}
if pkg.TypesInfo == nil || pkg.Fset == nil {
return
}
for i, file := range pkg.Syntax {
if i >= len(pkg.CompiledGoFiles) {
break
}
path := filepath.Clean(pkg.CompiledGoFiles[i])
if _, exists := out[path]; exists {
continue
}
out[path] = typedGoFile{File: file, Fset: pkg.Fset, Info: pkg.TypesInfo}
}
})
if packageErrCount == 1 {
return out, fmt.Errorf("load Go type information: %s", firstPackageErr)
}
if packageErrCount > 1 {
return out, fmt.Errorf(
"load Go type information: %s (and %d more package errors)",
firstPackageErr,
packageErrCount-1,
)
}
return out, nil
}
func newFileDomainScan(file typedGoFile) *fileDomainScan {
return &fileDomainScan{
File: file.File,
Fset: file.Fset,
Info: file.Info,
seen: map[evidenceKey]bool{},
parents: astParentMap(file.File),
}
}
func (s *fileDomainScan) collectSemanticEvidence() {
ast.Inspect(s.File, func(node ast.Node) bool {
switch n := node.(type) {
case *ast.AssignStmt:
if len(n.Lhs) != len(n.Rhs) {
return true
}
for i, lhs := range n.Lhs {
if s.Info == nil &&
potentialHostnameSelectorTarget(lhs) &&
s.hasStaticBareHostnameValue(n.Rhs[i]) {
s.requireTypeInfo(n.Rhs[i])
}
if index, ok := stripParens(lhs).(*ast.IndexExpr); ok {
switch {
case s.isHostnameTarget(index.X):
s.addMapPair(index.Index, n.Rhs[i])
case s.isHostnameMapKey(index.Index):
s.addHostValue(n.Rhs[i], "host assignment")
}
continue
}
if s.isHostnameTarget(lhs) {
s.addHostValue(n.Rhs[i], "host assignment")
}
}
case *ast.ValueSpec:
if len(n.Names) != len(n.Values) {
return true
}
for i, name := range n.Names {
if isHostnameSemanticName(name.Name) {
s.addHostValue(n.Values[i], "host assignment")
}
}
case *ast.KeyValueExpr:
if s.Info == nil && s.keyValueNeedsTypeInfo(n) {
s.requireTypeInfo(n.Value)
}
if s.isHostnameKeyValue(n) {
s.addHostValue(n.Value, "host assignment")
}
}
return true
})
}
func (s *fileDomainScan) requireTypeInfo(expr ast.Expr) {
for _, existing := range s.TypeInfoRequired {
if existing.Pos() == expr.Pos() && existing.End() == expr.End() {
return
}
}
s.TypeInfoRequired = append(s.TypeInfoRequired, expr)
}
func (s *fileDomainScan) hasStaticBareHostnameValue(expr ast.Expr) bool {
value, ok := staticStringValue(expr, s.Info, nil)
if !ok {
return false
}
host, ok := semanticHostname(value)
return ok && !isReservedExampleHostname(host)
}
func (s *fileDomainScan) keyValueNeedsTypeInfo(pair *ast.KeyValueExpr) bool {
composite, ok := s.parents[pair].(*ast.CompositeLit)
if !ok {
return false
}
if _, explicitMap := composite.Type.(*ast.MapType); explicitMap {
return false
}
key, ok := pair.Key.(*ast.Ident)
return ok && isHostnameSemanticName(key.Name) && s.hasStaticBareHostnameValue(pair.Value)
}
func potentialHostnameSelectorTarget(expr ast.Expr) bool {
switch n := stripParens(expr).(type) {
case *ast.SelectorExpr:
return isHostnameSemanticName(n.Sel.Name)
case *ast.StarExpr:
return potentialHostnameSelectorTarget(n.X)
case *ast.IndexExpr:
return potentialHostnameSelectorTarget(n.X)
default:
return false
}
}
func (s *fileDomainScan) collectAbsoluteURLEvidence() {
ast.Inspect(s.File, func(node ast.Node) bool {
expr, ok := node.(ast.Expr)
if !ok {
return true
}
if ident, ok := expr.(*ast.Ident); ok && s.Info != nil && s.Info.Defs[ident] != nil {
// A declaration name may carry the constant value in types.Info,
// but it is not a second source expression.
return true
}
value, ok := staticStringValue(expr, s.Info, nil)
if !ok {
return true
}
if s.hasStaticStringContainer(expr) {
return true
}
host, ok := absoluteURLHostname(value)
if ok {
s.addEvidence(host, "absolute URL", expr)
}
return true
})
}
func (s *fileDomainScan) hasStaticStringContainer(expr ast.Expr) bool {
parent, ok := s.parents[expr].(ast.Expr)
if !ok {
return false
}
switch parent.(type) {
case *ast.BinaryExpr, *ast.ParenExpr:
_, ok := staticStringValue(parent, s.Info, nil)
return ok
default:
return false
}
}
func (s *fileDomainScan) addHostValue(expr ast.Expr, kind string) {
expr = stripParens(expr)
if composite, ok := expr.(*ast.CompositeLit); ok {
switch s.collectionCompositeKind(composite) {
case sequenceComposite:
for _, element := range composite.Elts {
if valueExpr, ok := element.(ast.Expr); ok {
s.addHostValue(valueExpr, "host collection")
}
}
case mapComposite:
for _, element := range composite.Elts {
pair, ok := element.(*ast.KeyValueExpr)
if !ok {
continue
}
keyExpr, ok := pair.Key.(ast.Expr)
if !ok {
continue
}
s.addMapPair(keyExpr, pair.Value)
}
default:
if s.Info == nil {
s.requireTypeInfoForUnclassifiedCollection(composite)
}
return
}
return
}
if evidence, ok := s.hostnameEvidence(expr, kind); ok {
s.addEvidence(evidence.Host, evidence.Kind, evidence.Expr)
}
}
func (s *fileDomainScan) requireTypeInfoForUnclassifiedCollection(composite *ast.CompositeLit) {
for _, element := range composite.Elts {
if pair, ok := element.(*ast.KeyValueExpr); ok {
keyExpr, ok := pair.Key.(ast.Expr)
if !ok {
continue
}
keyIsHost := s.hasStaticBareHostnameValue(keyExpr)
valueIsHost := s.hasStaticBareHostnameValue(pair.Value)
if keyIsHost == valueIsHost {
continue
}
if keyIsHost {
s.requireTypeInfo(keyExpr)
} else {
s.requireTypeInfo(pair.Value)
}
continue
}
valueExpr, ok := element.(ast.Expr)
if ok && s.hasStaticBareHostnameValue(valueExpr) {
s.requireTypeInfo(valueExpr)
}
}
}
// addMapPair reports a map side only when it is the sole hostname-shaped
// static value. A semantic map name does not establish whether a string map
// is hostname->metadata or alias->hostname, so reporting both sides would turn
// filenames such as client.pem into blocking hostname evidence.
func (s *fileDomainScan) addMapPair(key, value ast.Expr) {
keyEvidence, keyOK := s.hostnameEvidence(key, "host collection")
valueEvidence, valueOK := s.hostnameEvidence(value, "host collection")
if keyOK == valueOK {
return
}
if keyOK {
s.addEvidence(keyEvidence.Host, keyEvidence.Kind, keyEvidence.Expr)
return
}
s.addEvidence(valueEvidence.Host, valueEvidence.Kind, valueEvidence.Expr)
}
func (s *fileDomainScan) hostnameEvidence(expr ast.Expr, kind string) (domainEvidence, bool) {
expr = stripParens(expr)
value, ok := staticStringValue(expr, s.Info, nil)
if !ok {
return domainEvidence{}, false
}
if host, ok := absoluteURLHostname(value); ok {
return domainEvidence{Host: host, Kind: "absolute URL", Expr: expr}, true
}
if host, ok := semanticHostname(value); ok {
return domainEvidence{Host: host, Kind: kind, Expr: expr}, true
}
return domainEvidence{}, false
}
func (s *fileDomainScan) collectionCompositeKind(expr *ast.CompositeLit) collectionCompositeKind {
if s.Info != nil {
if tv, ok := s.Info.Types[expr]; ok && tv.Type != nil {
switch tv.Type.Underlying().(type) {
case *types.Array, *types.Slice:
return sequenceComposite
case *types.Map:
return mapComposite
}
}
}
switch expr.Type.(type) {
case *ast.ArrayType:
return sequenceComposite
case *ast.MapType:
return mapComposite
default:
return notCollectionComposite
}
}
func (s *fileDomainScan) addEvidence(host, kind string, expr ast.Expr) {
key := evidenceKey{Host: host, Start: expr.Pos(), End: expr.End()}
if s.seen[key] {
return
}
s.seen[key] = true
s.Evidence = append(s.Evidence, domainEvidence{Host: host, Kind: kind, Expr: expr})
}
func staticStringValue(expr ast.Expr, info *types.Info, seen map[*ast.Object]bool) (string, bool) {
if info != nil {
if tv, ok := info.Types[expr]; ok && tv.Value != nil && tv.Value.Kind() == constant.String {
return constant.StringVal(tv.Value), true
}
}
switch n := expr.(type) {
case *ast.BasicLit:
if n.Kind != token.STRING {
return "", false
}
value, err := strconv.Unquote(n.Value)
return value, err == nil
case *ast.ParenExpr:
return staticStringValue(n.X, info, seen)
case *ast.BinaryExpr:
if n.Op != token.ADD {
return "", false
}
left, ok := staticStringValue(n.X, info, seen)
if !ok {
return "", false
}
right, ok := staticStringValue(n.Y, info, seen)
if !ok {
return "", false
}
return left + right, true
case *ast.Ident:
if info != nil {
if obj := info.ObjectOf(n); obj != nil {
if c, ok := obj.(*types.Const); ok {
if c.Val().Kind() == constant.String {
return constant.StringVal(c.Val()), true
}
}
}
}
if n.Obj == nil || n.Obj.Kind != ast.Con {
return "", false
}
if seen == nil {
seen = map[*ast.Object]bool{}
}
if seen[n.Obj] {
return "", false
}
seen[n.Obj] = true
defer delete(seen, n.Obj)
spec, ok := n.Obj.Decl.(*ast.ValueSpec)
if !ok {
return "", false
}
for i, name := range spec.Names {
if name.Name == n.Name && i < len(spec.Values) {
return staticStringValue(spec.Values[i], info, seen)
}
}
}
return "", false
}
func absoluteURLHostname(value string) (string, bool) {
value = strings.TrimSpace(value)
parsed, err := url.Parse(value)
if err != nil || parsed.Host == "" {
return "", false
}
switch strings.ToLower(parsed.Scheme) {
case "http", "https", "ws", "wss":
default:
return "", false
}
return normalizeCandidateHostname(parsed.Hostname())
}
func semanticHostname(value string) (string, bool) {
value = strings.TrimSpace(value)
if value == "" || strings.ContainsAny(value, `/\?#@`) || strings.ContainsAny(value, " \t\r\n") {
return "", false
}
parsed, err := url.Parse("//" + value)
if err != nil || parsed.Host == "" || parsed.Path != "" {
return "", false
}
return normalizeCandidateHostname(parsed.Hostname())
}
func normalizeCandidateHostname(host string) (string, bool) {
host = strings.TrimSuffix(strings.ToLower(strings.TrimSpace(host)), ".")
if host == "" || !strings.Contains(host, ".") || net.ParseIP(host) != nil {
return "", false
}
labels := strings.Split(host, ".")
for _, label := range labels {
if label == "" || strings.HasPrefix(label, "-") || strings.HasSuffix(label, "-") {
return "", false
}
for _, r := range label {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' {
continue
}
return "", false
}
}
return host, true
}
func (s *fileDomainScan) isHostnameTarget(expr ast.Expr) bool {
switch n := stripParens(expr).(type) {
case *ast.Ident:
return isHostnameSemanticName(n.Name)
case *ast.SelectorExpr:
return s.isHostnameSelector(n)
case *ast.StarExpr:
return s.isHostnameTarget(n.X)
default:
return false
}
}
func (s *fileDomainScan) isHostnameKeyValue(pair *ast.KeyValueExpr) bool {
composite, ok := s.parents[pair].(*ast.CompositeLit)
if !ok {
return false
}
switch s.collectionCompositeKind(composite) {
case mapComposite:
key, ok := pair.Key.(ast.Expr)
return ok && s.isHostnameMapKey(key)
case notCollectionComposite:
ident, ok := pair.Key.(*ast.Ident)
return ok && s.isHostnameStructField(composite, ident.Name)
default:
return false
}
}
func (s *fileDomainScan) isHostnameMapKey(expr ast.Expr) bool {
value, ok := staticStringValue(expr, s.Info, nil)
return ok && isHostnameSemanticName(value)
}
func (s *fileDomainScan) isHostnameSelector(selector *ast.SelectorExpr) bool {
if s.Info == nil || !isHostnameSemanticName(selector.Sel.Name) {
return false
}
selection := s.Info.Selections[selector]
if selection == nil || selection.Kind() != types.FieldVal {
return false
}
return !nonNetworkHostnameFields[hostnameFieldID{
Type: namedTypeID(selection.Recv()),
Field: selector.Sel.Name,
}]
}
func (s *fileDomainScan) isHostnameStructField(composite *ast.CompositeLit, field string) bool {
if s.Info == nil || !isHostnameSemanticName(field) {
return false
}
typeID := namedTypeID(s.Info.TypeOf(composite))
if typeID == "" {
return false
}
return !nonNetworkHostnameFields[hostnameFieldID{Type: typeID, Field: field}]
}
func namedTypeID(typ types.Type) string {
for {
switch t := typ.(type) {
case *types.Pointer:
typ = t.Elem()
case *types.Named:
obj := t.Obj()
if obj == nil || obj.Pkg() == nil {
return ""
}
return obj.Pkg().Path() + "." + obj.Name()
default:
return ""
}
}
}
func isHostnameSemanticName(name string) bool {
lower := strings.ToLower(name)
switch lower {
case "host", "hosts", "hostname", "hostnames", "domain", "domains":
return true
}
for _, marker := range []string{
"HostBy", "HostsBy", "HostnameBy", "HostnamesBy", "DomainBy", "DomainsBy",
} {
if i := strings.Index(name, marker); i >= 0 {
end := i + len(marker)
if end < len(name) && unicode.IsUpper(rune(name[end])) {
return true
}
}
}
for _, prefix := range []string{
"hostBy", "hostsBy", "hostnameBy", "hostnamesBy", "domainBy", "domainsBy",
} {
if strings.HasPrefix(name, prefix) &&
len(name) > len(prefix) &&
unicode.IsUpper(rune(name[len(prefix)])) {
return true
}
}
if i := strings.LastIndexAny(name, "_-"); i >= 0 {
return isHostnameSemanticName(name[i+1:])
}
for _, suffix := range []string{"Hostnames", "Hostname", "Domains", "Domain", "Hosts", "Host"} {
if strings.HasSuffix(name, suffix) && len(name) > len(suffix) {
return true
}
}
return false
}
func stripParens(expr ast.Expr) ast.Expr {
for {
paren, ok := expr.(*ast.ParenExpr)
if !ok {
return expr
}
expr = paren.X
}
}
func astParentMap(root ast.Node) map[ast.Node]ast.Node {
parents := map[ast.Node]ast.Node{}
var stack []ast.Node
ast.Inspect(root, func(node ast.Node) bool {
if node == nil {
stack = stack[:len(stack)-1]
return false
}
if len(stack) > 0 {
parents[node] = stack[len(stack)-1]
}
stack = append(stack, node)
return true
})
return parents
}
func isDomainFixturePath(rel string) bool {
rel = filepath.ToSlash(rel)
if strings.HasPrefix(rel, "skills/") {
return false
}
if strings.HasSuffix(rel, "_test.go") || strings.HasPrefix(rel, "tests/") {
return true
}
for _, part := range strings.Split(rel, "/") {
if part == "testdata" {
return true
}
}
return false
}
func unusedDomainViolation(entry domainPolicyEntry) lintapi.Violation {
return lintapi.Violation{
Rule: unusedDomainRule,
Action: lintapi.ActionReject,
File: entry.File,
Line: entry.Line,
Message: fmt.Sprintf("domain allowlist entry %q has no in-scope Go reference", entry.Host),
Suggestion: "remove the unused entry; allowlist entries must be justified by a current in-scope reference",
}
}
func incompleteDomainViolation(file string, err error) lintapi.Violation {
return incompleteDomainViolationAt(file, 1, err)
}
func incompleteDomainViolationAt(file string, line int, err error) lintapi.Violation {
return lintapi.Violation{
Rule: incompleteDomainRule,
Action: lintapi.ActionReject,
File: file,
Line: line,
Message: "domain scan incomplete: " + err.Error(),
Suggestion: "fix the Go parse or type-loading error so hostname analysis can complete",
}
}

View File

@@ -0,0 +1,462 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import (
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/larksuite/cli/lint/lintapi"
)
func gitTestCommand(t *testing.T, root string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = root
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
return strings.TrimSpace(string(out))
}
func setupDomainDiffRepo(t *testing.T, target string) (root, base string) {
t.Helper()
root = t.TempDir()
writeFile(t, root, "go.mod", "module example.com/domainfixture\n\ngo 1.23.0\n")
writeFile(t, root, publicDomainsPath, "# public\npublic.example.com\n")
writeFile(t, root, fixtureDomainsPath, "# fixtures\nfixture.example.com\n")
writeFile(t, root, "policy_refs.go", "package sample\n\nvar APIHost = \"public.example.com\"\n")
writeFile(t, root, "policy_refs_test.go", "package sample\n\nvar FixtureHost = \"fixture.example.com\"\n")
writeFile(t, root, "target.go", target)
gitTestCommand(t, root, "init", "-q")
gitTestCommand(t, root, "config", "user.name", "Domain Contract Test")
gitTestCommand(t, root, "config", "user.email", "domain-contract@example.com")
gitTestCommand(t, root, "add", ".")
gitTestCommand(t, root, "-c", "commit.gpgsign=false", "commit", "-qm", "base")
return root, gitTestCommand(t, root, "rev-parse", "HEAD")
}
func commitDomainDiff(t *testing.T, root, message string) {
t.Helper()
gitTestCommand(t, root, "add", "-A")
gitTestCommand(t, root, "-c", "commit.gpgsign=false", "commit", "-qm", message)
}
func violationsForRule(vs []lintapi.Violation, rule string) []lintapi.Violation {
var out []lintapi.Violation
for _, v := range vs {
if v.Rule == rule {
out = append(out, v)
}
}
return out
}
func scanDomainDiff(t *testing.T, root, base string) []lintapi.Violation {
t.Helper()
vs, err := ScanRepoWithOptions(root, ScanOptions{ChangedFrom: base})
if err != nil {
t.Fatal(err)
}
return vs
}
func TestUnapprovedDomainDiffContract(t *testing.T) {
t.Run("new PR 1975 case", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nvar APIHost = \"internal-api-drive-stream.larkoffice.com\"\n")
commitDomainDiff(t, root, "add internal host")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "internal-api-drive-stream.larkoffice.com") {
t.Fatalf("violations = %+v, want PR 1975 hostname", got)
}
})
t.Run("hostname field in nested Go module", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "nested/go.mod", "module example.com/nested\n\ngo 1.23.0\n")
writeFile(t, root, "nested/target.go",
"package nested\n\ntype Config struct{ Host string }\n\n"+
"var config = Config{Host: \"private.corp.internal\"}\n")
commitDomainDiff(t, root, "add nested module hostname")
all := scanDomainDiff(t, root, base)
got := violationsForRule(all, unapprovedDomainRule)
if len(got) != 1 || filepath.ToSlash(got[0].File) != "nested/target.go" ||
!strings.Contains(got[0].Message, "private.corp.internal") {
t.Fatalf("violations = %+v, want nested-module hostname rejection", got)
}
if incomplete := violationsForRule(all, incompleteDomainRule); len(incomplete) != 0 {
t.Fatalf("nested module must have complete type information: %+v", incomplete)
}
})
t.Run("changed excluded field reports incomplete scan", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "excluded.go",
"//go:build domaincontract_never && !domaincontract_never\n\npackage sample\n\n"+
"type Config struct{ Host string }\n\n"+
"var config = Config{Host: \"private.corp.internal\"}\n")
commitDomainDiff(t, root, "add excluded hostname field")
all := scanDomainDiff(t, root, base)
got := violationsForRule(all, incompleteDomainRule)
if len(got) != 1 || filepath.Base(got[0].File) != "excluded.go" || got[0].Line != 7 {
t.Fatalf("violations = %+v, want changed field scan-incomplete at line 7", got)
}
if unapproved := violationsForRule(all, unapprovedDomainRule); len(unapproved) != 0 {
t.Fatalf("untyped field must not produce an unverified hostname finding: %+v", unapproved)
}
})
t.Run("changed excluded selector reports incomplete scan", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "excluded.go",
"//go:build domaincontract_never && !domaincontract_never\n\npackage sample\n\n"+
"type Config struct{ Host string }\n\n"+
"func configure(config *Config) { config.Host = \"private.corp.internal\" }\n")
commitDomainDiff(t, root, "add excluded hostname selector")
got := violationsForRule(scanDomainDiff(t, root, base), incompleteDomainRule)
if len(got) != 1 || filepath.Base(got[0].File) != "excluded.go" || got[0].Line != 7 {
t.Fatalf("violations = %+v, want changed selector scan-incomplete at line 7", got)
}
})
t.Run("changed excluded named slice reports incomplete scan", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "excluded.go",
"//go:build domaincontract_never && !domaincontract_never\n\npackage sample\n\n"+
"type HostList []string\n\n"+
"var AllowedHosts = HostList{\n\t\"attacker.zip\",\n}\n")
commitDomainDiff(t, root, "add excluded hostname slice")
all := scanDomainDiff(t, root, base)
got := violationsForRule(all, incompleteDomainRule)
if len(got) != 1 || filepath.Base(got[0].File) != "excluded.go" || got[0].Line != 8 {
t.Fatalf("violations = %+v, want named-slice scan-incomplete at line 8", got)
}
if unapproved := violationsForRule(all, unapprovedDomainRule); len(unapproved) != 0 {
t.Fatalf("untyped named slice must not produce an unverified hostname finding: %+v", unapproved)
}
})
t.Run("changed excluded named map reports incomplete scan", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "excluded.go",
"//go:build domaincontract_never && !domaincontract_never\n\npackage sample\n\n"+
"type HostSet map[string]struct{}\n\n"+
"var AllowedHosts = HostSet{\n\t\"attacker.zip\": {},\n}\n")
commitDomainDiff(t, root, "add excluded hostname map")
all := scanDomainDiff(t, root, base)
got := violationsForRule(all, incompleteDomainRule)
if len(got) != 1 || filepath.Base(got[0].File) != "excluded.go" || got[0].Line != 8 {
t.Fatalf("violations = %+v, want named-map scan-incomplete at line 8", got)
}
if unapproved := violationsForRule(all, unapprovedDomainRule); len(unapproved) != 0 {
t.Fatalf("untyped named map must not produce an unverified hostname finding: %+v", unapproved)
}
})
t.Run("changed excluded unrelated code stays allowed", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "excluded.go",
"//go:build domaincontract_never && !domaincontract_never\n\npackage sample\n\nvar unrelated = 2\n")
commitDomainDiff(t, root, "add excluded unrelated code")
if got := violationsForRule(scanDomainDiff(t, root, base), incompleteDomainRule); len(got) != 0 {
t.Fatalf("unrelated excluded code must not require hostname type information: %+v", got)
}
})
t.Run("new element in existing collection", func(t *testing.T) {
root, base := setupDomainDiffRepo(t,
"package sample\n\nvar ExtraHosts = []string{\n\t\"public.example.com\",\n}\n")
writeFile(t, root, "target.go",
"package sample\n\nvar ExtraHosts = []string{\n\t\"public.example.com\",\n\t\"attacker.zip\",\n}\n")
commitDomainDiff(t, root, "add collection host")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "attacker.zip") {
t.Fatalf("violations = %+v, want attacker.zip", got)
}
if got[0].Line != 5 {
t.Fatalf("violation line = %d, want 5", got[0].Line)
}
})
t.Run("multiline expression changed segment", func(t *testing.T) {
root, base := setupDomainDiffRepo(t,
"package sample\n\nvar ExtraHost = \"private.corp.\" +\n\t\"example.com\"\n")
writeFile(t, root, "target.go",
"package sample\n\nvar ExtraHost = \"private.corp.\" +\n\t\"internal\"\n")
commitDomainDiff(t, root, "change concatenated host")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "private.corp.internal") {
t.Fatalf("violations = %+v, want private.corp.internal", got)
}
if got[0].Line != 4 {
t.Fatalf("violation line = %d, want changed line 4", got[0].Line)
}
})
t.Run("unrelated change beside historical hostname", func(t *testing.T) {
root, base := setupDomainDiffRepo(t,
"package sample\n\nvar HistoricalHost = \"historical.private.internal\"\n")
writeFile(t, root, "target.go",
"package sample\n\nvar HistoricalHost = \"historical.private.internal\"\nvar unrelated = 1\n")
commitDomainDiff(t, root, "add unrelated value")
if got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule); len(got) != 0 {
t.Fatalf("unexpected historical-domain violation: %+v", got)
}
})
t.Run("historical hostname expression changed", func(t *testing.T) {
root, base := setupDomainDiffRepo(t,
"package sample\n\nvar HistoricalHost = \"historical.private.internal\"\n")
writeFile(t, root, "target.go",
"package sample\n\nvar HistoricalHost = \"replacement.private.internal\"\n")
commitDomainDiff(t, root, "change historical host")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "replacement.private.internal") {
t.Fatalf("violations = %+v, want replacement.private.internal", got)
}
})
t.Run("new assignment references existing constant", func(t *testing.T) {
root, base := setupDomainDiffRepo(t,
"package sample\n\nconst existingConst = \"private.corp.internal\"\n")
writeFile(t, root, "target.go",
"package sample\n\nconst existingConst = \"private.corp.internal\"\nvar APIHost = existingConst\n")
commitDomainDiff(t, root, "use existing hostname constant")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "private.corp.internal") {
t.Fatalf("violations = %+v, want private.corp.internal", got)
}
if got[0].Line != 4 {
t.Fatalf("violation line = %d, want 4", got[0].Line)
}
})
t.Run("allowlisted hostname", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nvar BackupHost = \"public.example.com\"\n")
commitDomainDiff(t, root, "add public host")
if got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule); len(got) != 0 {
t.Fatalf("unexpected public-domain violation: %+v", got)
}
})
t.Run("reserved example URL", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nfunc fakeValue() string { return \"https://example.test/resource\" }\n")
commitDomainDiff(t, root, "add safe example URL")
if got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule); len(got) != 0 {
t.Fatalf("unexpected reserved-example violation: %+v", got)
}
})
t.Run("historical type gap suppresses unused policy diagnostics", func(t *testing.T) {
root, _ := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, publicDomainsPath,
"# public\nplatform.example.com\npublic.example.com\n")
writeFile(t, root, "excluded.go",
"//go:build domaincontract_never && !domaincontract_never\n\npackage sample\n\n"+
"type Config struct{ Host string }\n\n"+
"var config = Config{Host: \"platform.example.com\"}\n")
commitDomainDiff(t, root, "add historical platform hostname")
base := gitTestCommand(t, root, "rev-parse", "HEAD")
writeFile(t, root, "target.go", "package sample\n\nvar unrelated = 2\n")
commitDomainDiff(t, root, "change unrelated code")
all := scanDomainDiff(t, root, base)
if got := violationsForRule(all, incompleteDomainRule); len(got) != 0 {
t.Fatalf("historical type gap must not be attributed to this change: %+v", got)
}
if got := violationsForRule(all, unusedDomainRule); len(got) != 0 {
t.Fatalf("incomplete inventory must not produce unused-policy diagnostics: %+v", got)
}
})
t.Run("allowlist does not approve subdomains", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nvar BackupHost = \"evil.public.example.com\"\n")
commitDomainDiff(t, root, "add unapproved public subdomain")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "evil.public.example.com") {
t.Fatalf("violations = %+v, want evil.public.example.com", got)
}
})
t.Run("multi assignment pairs names and values", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, publicDomainsPath,
"# public\nopen.larksuite.com\npublic.example.com\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nvar APIHost, BackupHost = \"open.larksuite.com\", \"attacker.zip\"\n")
commitDomainDiff(t, root, "add multiple hosts")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "attacker.zip") {
t.Fatalf("violations = %+v, want only attacker.zip", got)
}
})
t.Run("IDN hostname is rejected", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nvar BackupHost = \"例子.公司.cn\"\n")
commitDomainDiff(t, root, "add IDN hostname")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "例子.公司.cn") {
t.Fatalf("violations = %+v, want IDN hostname", got)
}
})
t.Run("fixture limited to test files", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go",
"package sample\n\nvar unrelated = 1\nvar ProductionHost = \"fixture.example.com\"\n")
commitDomainDiff(t, root, "use fixture in production")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "fixture.example.com") {
t.Fatalf("violations = %+v, want production fixture rejection", got)
}
if !strings.Contains(got[0].Suggestion, "fixture-only hostname") ||
strings.Contains(got[0].Suggestion, "public allowlist") {
t.Fatalf("suggestion = %q, want fixture-scope guidance", got[0].Suggestion)
}
})
t.Run("fixture accepted in test file", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "new_target_test.go",
"package sample\n\nvar BackupHost = \"fixture.example.com\"\n")
commitDomainDiff(t, root, "use fixture in test")
if got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule); len(got) != 0 {
t.Fatalf("unexpected fixture-domain violation: %+v", got)
}
})
t.Run("fixture allowlist does not approve subdomains", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "new_target_test.go",
"package sample\n\nvar BackupHost = \"evil.fixture.example.com\"\n")
commitDomainDiff(t, root, "use unapproved fixture subdomain")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "evil.fixture.example.com") {
t.Fatalf("violations = %+v, want exact fixture match", got)
}
})
t.Run("fixture rejected in skills", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "skills/example/example_test.go",
"package example\n\nvar BackupHost = \"fixture.example.com\"\n")
commitDomainDiff(t, root, "use fixture in skill")
got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "fixture.example.com") {
t.Fatalf("violations = %+v, want skill fixture rejection", got)
}
})
t.Run("pure rename", func(t *testing.T) {
root, base := setupDomainDiffRepo(t,
"package sample\n\nvar HistoricalHost = \"historical.private.internal\"\n")
gitTestCommand(t, root, "mv", "target.go", "renamed.go")
commitDomainDiff(t, root, "rename file")
if got := violationsForRule(scanDomainDiff(t, root, base), unapprovedDomainRule); len(got) != 0 {
t.Fatalf("unexpected rename violation: %+v", got)
}
})
}
func TestUnapprovedDomainPolicyAndFailurePaths(t *testing.T) {
t.Run("unused policy entry", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, publicDomainsPath,
"# public\npublic.example.com\nunused.example.com\n")
commitDomainDiff(t, root, "add unused policy")
got := violationsForRule(scanDomainDiff(t, root, base), unusedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "unused.example.com") {
t.Fatalf("violations = %+v, want unused.example.com", got)
}
})
t.Run("public entry used only by fixture", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, publicDomainsPath,
"# public\npublic.example.com\ntest-only.example.com\n")
writeFile(t, root, "public_only_test.go",
"package sample\n\nvar BackupHost = \"test-only.example.com\"\n")
commitDomainDiff(t, root, "add test-only public policy")
got := violationsForRule(scanDomainDiff(t, root, base), unusedDomainRule)
if len(got) != 1 || !strings.Contains(got[0].Message, "test-only.example.com") {
t.Fatalf("violations = %+v, want test-only.example.com", got)
}
})
t.Run("changed Go parse failure", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "target.go", "package sample\n\nfunc broken(\n")
commitDomainDiff(t, root, "break source")
all := scanDomainDiff(t, root, base)
got := violationsForRule(all, incompleteDomainRule)
if len(got) != 1 || filepath.Base(got[0].File) != "target.go" {
t.Fatalf("violations = %+v, want target.go scan-incomplete", got)
}
if unused := violationsForRule(all, unusedDomainRule); len(unused) != 0 {
t.Fatalf("parse failure must not produce unreliable unused-policy diagnostics: %+v", unused)
}
})
t.Run("repository type loading failure", func(t *testing.T) {
root, base := setupDomainDiffRepo(t, "package sample\n\nvar unrelated = 1\n")
writeFile(t, root, "go.mod", "module example.com/domainfixture\n\ngo 1.23.0\n\n"+
"require example.com/missing v0.0.0\n\nreplace example.com/missing => ./missing\n")
writeFile(t, root, "target.go",
"package sample\n\nimport _ \"example.com/missing\"\n\n"+
"type Config struct{ Host string }\nvar config = Config{Host: \"malicious.corp.internal\"}\n")
commitDomainDiff(t, root, "break type loading")
all := scanDomainDiff(t, root, base)
got := violationsForRule(all, incompleteDomainRule)
if len(got) != 1 || filepath.Base(got[0].File) != "go.mod" {
t.Fatalf("violations = %+v, want go.mod scan-incomplete", got)
}
if !strings.Contains(got[0].Message, "load Go type information") {
t.Fatalf("message = %q, want type-loading failure", got[0].Message)
}
if unused := violationsForRule(all, unusedDomainRule); len(unused) != 0 {
t.Fatalf("type-loading failure must not produce unreliable unused-policy diagnostics: %+v", unused)
}
})
}

View File

@@ -0,0 +1,380 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package domaincontract
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"sort"
"testing"
)
func scanDomainEvidence(t *testing.T, source string) []domainEvidence {
t.Helper()
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "fixture.go", source, 0)
if err != nil {
t.Fatalf("parse fixture: %v\n%s", err, source)
}
scan := newFileDomainScan(typedGoFile{File: file, Fset: fset})
scan.collectSemanticEvidence()
scan.collectAbsoluteURLEvidence()
sort.Slice(scan.Evidence, func(i, j int) bool {
if scan.Evidence[i].Host != scan.Evidence[j].Host {
return scan.Evidence[i].Host < scan.Evidence[j].Host
}
return scan.Evidence[i].Expr.Pos() < scan.Evidence[j].Expr.Pos()
})
return scan.Evidence
}
func scanTypedDomainEvidence(t *testing.T, source string) []domainEvidence {
t.Helper()
return scanTypedDomainEvidenceInPackage(t, "fixture", source)
}
func scanTypedDomainEvidenceInPackage(t *testing.T, packagePath, source string) []domainEvidence {
t.Helper()
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "fixture.go", source, 0)
if err != nil {
t.Fatalf("parse fixture: %v\n%s", err, source)
}
info := &types.Info{
Types: map[ast.Expr]types.TypeAndValue{},
Defs: map[*ast.Ident]types.Object{},
Uses: map[*ast.Ident]types.Object{},
Selections: map[*ast.SelectorExpr]*types.Selection{},
}
if _, err := (&types.Config{}).Check(packagePath, fset, []*ast.File{file}, info); err != nil {
t.Fatalf("type-check fixture: %v\n%s", err, source)
}
scan := newFileDomainScan(typedGoFile{File: file, Fset: fset, Info: info})
scan.collectSemanticEvidence()
scan.collectAbsoluteURLEvidence()
sort.Slice(scan.Evidence, func(i, j int) bool {
if scan.Evidence[i].Host != scan.Evidence[j].Host {
return scan.Evidence[i].Host < scan.Evidence[j].Host
}
return scan.Evidence[i].Expr.Pos() < scan.Evidence[j].Expr.Pos()
})
return scan.Evidence
}
func evidenceHosts(evidence []domainEvidence) []string {
hosts := make([]string, 0, len(evidence))
for _, item := range evidence {
hosts = append(hosts, item.Host)
}
return hosts
}
func TestTypedAbsoluteURLDeclarationProducesOneFinding(t *testing.T) {
evidence := scanTypedDomainEvidence(t,
"package p\nconst DomainContractE2EURL = \"https://private.corp.internal/v1\"\n")
if got := evidenceHosts(evidence); len(got) != 1 || got[0] != "private.corp.internal" {
t.Fatalf("hosts = %v, want [private.corp.internal]", got)
}
}
func TestGoDomainEvidenceTruePositives(t *testing.T) {
tests := []struct {
name string
source string
want []string
}{
{
name: "PR 1975 Feishu assignment",
source: "package p\nfunc f() { host := \"internal-api-drive-stream.feishu.cn\"; _ = host }\n",
want: []string{"internal-api-drive-stream.feishu.cn"},
},
{
name: "PR 1975 Lark assignment",
source: "package p\nfunc f() { var host string; host = \"internal-api-drive-stream.larksuite.com\"; _ = host }\n",
want: []string{"internal-api-drive-stream.larksuite.com"},
},
{
name: "uppercase snake target",
source: "package p\nfunc f() { API_HOST := \"private.corp.internal\"; _ = API_HOST }\n",
want: []string{"private.corp.internal"},
},
{
name: "typed declaration",
source: "package p\nconst APIHost string = \"attacker.zip\"\n",
want: []string{"attacker.zip"},
},
{
name: "grouped const declaration",
source: "package p\nconst (\n APIHost string = \"attacker.zip\"\n)\n",
want: []string{"attacker.zip"},
},
{
name: "grouped var declaration",
source: "package p\nvar (\n APIHost string = \"attacker.zip\"\n)\n",
want: []string{"attacker.zip"},
},
{
name: "multi assignment",
source: "package p\nfunc f() {\n" +
" APIHost, BackupHost := \"public.example.com\", \"attacker.zip\"\n" +
" _, _ = APIHost, BackupHost\n}\n",
want: []string{"attacker.zip", "public.example.com"},
},
{
name: "map semantic key",
source: "package p\nvar c = map[string]string{\"host\": \"private.corp.internal\"}\n",
want: []string{"private.corp.internal"},
},
{
name: "map semantic key assignment",
source: "package p\nfunc f() { c := map[string]string{}; c[\"host\"] = \"private.corp.internal\" }\n",
want: []string{"private.corp.internal"},
},
{
name: "host collection values",
source: "package p\nvar ALLOWED_HOSTS = []string{\"private.corp.internal\", \"attacker.zip\"}\n",
want: []string{"attacker.zip", "private.corp.internal"},
},
{
name: "host collection map keys",
source: "package p\nvar allowedHosts = map[string]struct{}{\"attacker.zip\": {}}\n",
want: []string{"attacker.zip"},
},
{
name: "host collection bool map keys",
source: "package p\nvar AllowedHosts = map[string]bool{\"api.example.com\": true}\n",
want: []string{"api.example.com"},
},
{
name: "host collection map values",
source: "package p\nvar HostsByRegion = map[string]string{\"sg\": \"api.example.com\"}\n",
want: []string{"api.example.com"},
},
{
name: "host collection map value assignment",
source: "package p\nfunc f() {\n" +
" HostsByRegion := map[string]string{}\n" +
" HostsByRegion[\"sg\"] = \"api.example.com\"\n" +
"}\n",
want: []string{"api.example.com"},
},
{
name: "static concatenation",
source: "package p\nvar APIHost = \"attacker.\" + \"zip\"\n",
want: []string{"attacker.zip"},
},
{
name: "multiline assignment",
source: "package p\nfunc f() {\n APIHost :=\n \"attacker.zip\"\n _ = APIHost\n}\n",
want: []string{"attacker.zip"},
},
{
name: "escaped hostname",
source: "package p\nvar APIHost = \"private\\u002ecorp\\u002einternal\"\n",
want: []string{"private.corp.internal"},
},
{
name: "hex escaped hostname",
source: "package p\nvar APIHost = \"private\\x2ecorp\\x2einternal\"\n",
want: []string{"private.corp.internal"},
},
{
name: "octal escaped hostname",
source: "package p\nvar APIHost = \"private\\056corp\\056internal\"\n",
want: []string{"private.corp.internal"},
},
{
name: "raw hostname",
source: "package p\nvar APIHost = `private.corp.internal`\n",
want: []string{"private.corp.internal"},
},
{
name: "same-file constant reference",
source: "package p\nconst existingConst = \"private.corp.internal\"\n" +
"func f() { APIHost := existingConst; _ = APIHost }\n",
want: []string{"private.corp.internal"},
},
{
name: "absolute URL",
source: "package p\nvar message = \"https://private.corp.internal/v1\"\n",
want: []string{"private.corp.internal"},
},
{
name: "websocket URL with port",
source: "package p\nvar endpoint = \"wss://private.corp.internal:443/v1\"\n",
want: []string{"private.corp.internal"},
},
{
name: "URL userinfo query and fragment",
source: "package p\nvar endpoint = \" https://user:pass@private.corp.internal:8443/v1?q=1#result \"\n",
want: []string{"private.corp.internal"},
},
{
name: "IDN hostname",
source: "package p\nvar APIHost = \"例子.公司.cn\"\n",
want: []string{"例子.公司.cn"},
},
{
name: "case port and trailing dot normalization",
source: "package p\nvar APIHost = \"EXAMPLE.COM.:443\"\n",
want: []string{"example.com"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := evidenceHosts(scanDomainEvidence(t, tc.source))
if len(got) != len(tc.want) {
t.Fatalf("hosts = %v, want %v", got, tc.want)
}
for i := range got {
if got[i] != tc.want[i] {
t.Fatalf("hosts = %v, want %v", got, tc.want)
}
}
})
}
}
func TestGoDomainEvidenceTrueNegatives(t *testing.T) {
source := `package p
import _ "github.com/larksuite/oapi-sdk-go/v3"
var file = "archive.zip"
var event = "card.action.trigger"
var schema = "im.messages.list"
var configFile = "service.prod.json"
var version = "v1.2.3"
var email = "name@example.com"
var lowConfidence = "attacker.zip"
var downloadURL = "archive.zip/file"
var prose = "See https://private.corp.internal/v1 for details"
// https://private.corp.internal/v1
var ghost = "private.corp.internal"
var hostnameParser = "private.corp.internal"
var domainError = "private.corp.internal"
var APIHost = "localhost"
var BackupHost = "127.0.0.1"
var hosts = struct{ File string }{File: "archive.zip"}
var AllowedHosts = map[string]string{"api.example.com": "client.pem"}
func dynamicValue() string { return "private.corp.internal" }
var DynamicHost = dynamicValue()
func setAmbiguousHostMetadata() {
AllowedHosts["api.example.com"] = "client.pem"
}
`
if got := scanDomainEvidence(t, source); len(got) != 0 {
t.Fatalf("unexpected evidence: %+v", got)
}
}
func TestTypedStructFieldHostnameSemantics(t *testing.T) {
t.Run("network fields", func(t *testing.T) {
source := `package source
type Config struct { Host string }
type FeishuSource struct { Domain string }
var config = Config{Host: "api.example.com"}
var source = FeishuSource{Domain: "events.example.com"}
`
got := evidenceHosts(scanTypedDomainEvidenceInPackage(
t,
"github.com/larksuite/cli/internal/event/source",
source,
))
want := []string{"api.example.com", "events.example.com"}
if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
t.Fatalf("hosts = %v, want %v", got, want)
}
})
t.Run("command metadata domain", func(t *testing.T) {
source := `package cmdmeta
type Meta struct { Domain string }
var meta = Meta{Domain: "im.messages"}
func update(meta *Meta) { meta.Domain = "docs.pages" }
`
if got := scanTypedDomainEvidenceInPackage(
t,
"github.com/larksuite/cli/internal/cmdmeta",
source,
); len(got) != 0 {
t.Fatalf("unexpected command metadata evidence: %+v", got)
}
})
t.Run("card action host", func(t *testing.T) {
source := `package im
type CardActionTriggerOutput struct { Host string }
var output = CardActionTriggerOutput{Host: "card.action"}
func update(output *CardActionTriggerOutput) { output.Host = "im.message" }
`
if got := scanTypedDomainEvidenceInPackage(
t,
"github.com/larksuite/cli/events/im",
source,
); len(got) != 0 {
t.Fatalf("unexpected card host evidence: %+v", got)
}
})
t.Run("unknown field ownership is conservative", func(t *testing.T) {
source := "package p\ntype Config struct { Host string }\nvar c = Config{Host: \"api.example.com\"}\n"
if got := scanDomainEvidence(t, source); len(got) != 0 {
t.Fatalf("unexpected untyped field evidence: %+v", got)
}
})
}
func TestHostnameSemanticNames(t *testing.T) {
for _, name := range []string{
"host", "HOST", "hosts", "hostname", "domains",
"api_host", "API_HOST", "ALLOWED_HOSTS",
"apiHost", "APIHost", "backupHostname",
"HostsByRegion", "APIHostsByRegion", "hostsByRegion",
} {
if !isHostnameSemanticName(name) {
t.Errorf("%q should be hostname-semantic", name)
}
}
for _, name := range []string{
"ghost", "hostnameParser", "domainError", "hostValue", "downloadURL", "endpoint", "origin",
"HostBypass", "APIHostBypass",
} {
if isHostnameSemanticName(name) {
t.Errorf("%q must not be hostname-semantic", name)
}
}
}
func TestDomainFixturePaths(t *testing.T) {
for _, path := range []string{
"internal/x/x_test.go",
"tests/cli_e2e/x.go",
"internal/x/testdata/sample.go",
} {
if !isDomainFixturePath(path) {
t.Errorf("%q should be fixture scope", path)
}
}
for _, path := range []string{
"internal/x/test_helper.go",
"examples/demo.go",
"skills/example/testdata/sample.go",
"skills/example/example_test.go",
} {
if isDomainFixturePath(path) {
t.Errorf("%q must not be fixture scope", path)
}
}
}

View File

@@ -3,7 +3,7 @@
// Command lintcheck runs repository source-contract guards that golangci-lint
// cannot express directly. It currently covers typed-error contracts and the
// resolver-owned endpoint contract.
// resolver-owned endpoint and approved-domain contracts.
//
// lintcheck lives in its own Go module under lint/ so its build-time
// dependency on golang.org/x/tools/go/packages does not leak into the
@@ -43,8 +43,10 @@ type scanner struct {
var scanners = []scanner{
{name: "errscontract", fn: errscontract.ScanRepoWithOptions},
{name: "domaincontract", fn: func(root string, _ errscontract.ScanOptions) ([]lintapi.Violation, error) {
return domaincontract.ScanRepo(root)
{name: "domaincontract", fn: func(root string, opts errscontract.ScanOptions) ([]lintapi.Violation, error) {
return domaincontract.ScanRepoWithOptions(root, domaincontract.ScanOptions{
ChangedFrom: opts.ChangedFrom,
})
}},
}
@@ -57,7 +59,7 @@ func main() {
"Runs every registered lint domain against repo-root (default: current directory).\n")
flag.PrintDefaults()
}
flag.StringVar(&changedFrom, "changed-from", "", "base revision for incremental boundary-error checks")
flag.StringVar(&changedFrom, "changed-from", "", "base revision for incremental source-contract checks")
flag.BoolVar(&printLegacyCommandErrorCandidates, "print-legacy-command-error-candidates", false, "print existing command boundary bare errors as allowlist candidates")
flag.Parse()

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@larksuite/cli",
"version": "1.0.77",
"version": "1.0.80",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@larksuite/cli",
"version": "1.0.77",
"version": "1.0.80",
"cpu": [
"x64",
"arm64",

View File

@@ -1,6 +1,6 @@
{
"name": "@larksuite/cli",
"version": "1.0.77",
"version": "1.0.80",
"description": "The official CLI for Lark/Feishu open platform",
"bin": {
"lark-cli": "scripts/run.js"

View File

@@ -176,7 +176,15 @@ if ! grep -Fq "if: always() && github.event.workflow_run.conclusion == 'success'
exit 1
fi
require_in_step "$summary_verify_step" 'workflowPath !== ".github/workflows/ci.yml"' "PR quality summary must verify the triggering workflow path"
if grep -Fq 'run.name !== "CI"' "$workflow"; then
echo "semantic-review must not use the dynamic workflow run name as workflow identity" >&2
exit 1
fi
require_in_step "$summary_verify_step" 'github.rest.actions.getWorkflow' "PR quality summary must resolve static workflow metadata"
require_in_step "$summary_verify_step" 'workflow.name !== "CI"' "PR quality summary must verify the static workflow name"
require_in_step "$summary_verify_step" 'workflow.path !== ".github/workflows/ci.yml"' "PR quality summary must verify the static workflow path"
require_in_step "$summary_verify_step" 'run.path && run.path !== workflow.path' "PR quality summary must reject workflow path metadata mismatches"
require_in_step "$summary_verify_step" 'run.event !== "pull_request"' "PR quality summary must only handle pull_request workflow_run events"
require_in_step "$summary_verify_step" 'run.repository.id !== context.payload.repository.id' "PR quality summary must verify workflow_run repository id"
require_in_step "$summary_verify_step" 'const targetHeadSha = run.head_sha' "PR quality summary must use the CI run head SHA as the verified PR head"
@@ -201,7 +209,10 @@ require_in_step "$summary_publish_step" 'CI_QUALITY_SUMMARY_BASE_SHA' "PR qualit
require_in_step "$summary_publish_step" 'CI_QUALITY_SUMMARY_RUN_ID' "PR quality summary publisher must receive verified workflow run id"
require_in_step "$summary_publish_step" 'require("./scripts/ci-quality-summary-publish.js")' "PR quality summary publisher must use the shared CI publisher script"
require_in_step "$verify_step" 'workflowPath !== ".github/workflows/ci.yml"' "semantic-review must verify the triggering workflow path"
require_in_step "$verify_step" 'github.rest.actions.getWorkflow' "semantic-review must resolve static workflow metadata"
require_in_step "$verify_step" 'workflow.name !== "CI"' "semantic-review must verify the static workflow name"
require_in_step "$verify_step" 'workflow.path !== ".github/workflows/ci.yml"' "semantic-review must verify the static workflow path"
require_in_step "$verify_step" 'run.path && run.path !== workflow.path' "semantic-review must reject workflow path metadata mismatches"
require_in_step "$verify_step" 'run.repository.id !== context.payload.repository.id' "semantic-review must verify workflow_run repository id"
require_in_step "$verify_step" 'run.event !== "pull_request"' "semantic-review must only handle pull_request workflow_run events"
require_in_step "$verify_step" 'run.conclusion !== "success"' "semantic-review must only consume successful CI runs"

View File

@@ -0,0 +1,71 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"context"
"fmt"
"io"
"github.com/larksuite/cli/shortcuts/common"
)
// AppsCacheClear clears all cache entries for the app in the given environment.
//
// POST /apps/{app_id}/cache/clearbody {env}。清空当前应用指定环境下全部缓存,用于无法定位
// 具体 key 的快速恢复;影响面大,定 high-risk-write框架自动注入 --yes 确认)。
var AppsCacheClear = common.Shortcut{
Service: appsService,
Command: "+cache-clear",
Description: "Clear all cache entries for the app in the given environment",
Risk: "high-risk-write",
Tips: []string{
"Example: lark-cli apps +cache-clear --app-id <app_id> --environment dev --yes",
},
Scopes: []string{"spark:app:write"},
AuthTypes: []string{"user"},
HasFormat: true,
Flags: []common.Flag{
{Name: "app-id", Desc: "Miaoda app id", Required: true},
cacheEnvFlag(),
},
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
_, err := requireAppID(rctx.Str("app-id"))
return err
},
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
appID, _ := requireAppID(rctx.Str("app-id"))
return common.NewDryRunAPI().
POST(appCacheClearPath(appID)).
Desc("Clear all cache entries for the app in the given environment").
Body(dbEnvParams(rctx, map[string]interface{}{}))
},
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
appID, err := requireAppID(rctx.Str("app-id"))
if err != nil {
return err
}
data, err := rctx.CallAPITyped("POST", appCacheClearPath(appID), nil, dbEnvParams(rctx, map[string]interface{}{}))
if err != nil {
return withAppsHint(err, appIDListHint)
}
out := map[string]interface{}{
"environment": resolvedEnv(data, rctx),
"deleted_key_count": cacheInt(data["deleted_key_count"]),
}
rctx.OutFormat(out, nil, func(w io.Writer) {
renderCacheClearPretty(w, out)
})
return nil
},
}
// renderCacheClearPretty 打 "✓ cache cleared: N entries (env)"。
func renderCacheClearPretty(w io.Writer, out map[string]interface{}) {
n := int64(0)
if f, ok := numericAsFloat(out["deleted_key_count"]); ok {
n = int64(f)
}
fmt.Fprintf(w, "✓ cache cleared: %d entries (%s)\n", n, common.GetString(out, "environment"))
}

View File

@@ -0,0 +1,75 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"context"
"fmt"
"io"
"github.com/larksuite/cli/shortcuts/common"
)
// AppsCacheDelete deletes a single business cache key (idempotent).
//
// DELETE /apps/{app_id}/cache?env=&key=。缓存是派生数据、删单 key 影响面小且可重建,
// 故定 write非 high-risk-write、不需 --yes。目标不存在按幂等成功处理deleted_key_count=0
var AppsCacheDelete = common.Shortcut{
Service: appsService,
Command: "+cache-delete",
Description: "Delete a single business cache key (idempotent)",
Risk: "write",
Tips: []string{
"Example: lark-cli apps +cache-delete --app-id <app_id> --environment dev --key <key>",
},
Scopes: []string{"spark:app:write"},
AuthTypes: []string{"user"},
HasFormat: true,
Flags: []common.Flag{
{Name: "app-id", Desc: "Miaoda app id", Required: true},
{Name: "key", Desc: "business cache key", Required: true},
cacheEnvFlag(),
},
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
_, err := requireAppID(rctx.Str("app-id"))
return err
},
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
appID, _ := requireAppID(rctx.Str("app-id"))
return common.NewDryRunAPI().
DELETE(appCachePath(appID)).
Desc("Delete a Miaoda app runtime cache key").
Params(dbEnvParams(rctx, map[string]interface{}{"key": rctx.Str("key")}))
},
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
appID, err := requireAppID(rctx.Str("app-id"))
if err != nil {
return err
}
key := rctx.Str("key")
data, err := rctx.CallAPITyped("DELETE", appCachePath(appID), dbEnvParams(rctx, map[string]interface{}{"key": key}), nil)
if err != nil {
return withAppsHint(err, appIDListHint)
}
out := map[string]interface{}{
"key": key,
"environment": resolvedEnv(data, rctx),
"deleted_key_count": cacheInt(data["deleted_key_count"]),
}
rctx.OutFormat(out, nil, func(w io.Writer) {
renderCacheDeletePretty(w, out)
})
return nil
},
}
// renderCacheDeletePretty 命中打 "✓ cache deleted",幂等未命中打 "✓ cache already absent"(措辞区分,都成功)。
func renderCacheDeletePretty(w io.Writer, out map[string]interface{}) {
key := common.GetString(out, "key")
if n, ok := numericAsFloat(out["deleted_key_count"]); ok && n > 0 {
fmt.Fprintf(w, "✓ cache deleted: %s\n", key)
return
}
fmt.Fprintf(w, "✓ cache already absent: %s\n", key)
}

View File

@@ -0,0 +1,105 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"context"
"fmt"
"io"
"github.com/larksuite/cli/shortcuts/common"
)
// AppsCacheGet reads a single business cache key's value + metadata.
//
// GET /apps/{app_id}/cache?env=&key=。value 在 wire 上是 JSON 字符串透传:--format json
// 原样输出该字符串(不反序列化),--format pretty 反序列化后缩进展开。value_size_bytes 由 CLI
// 按 value 字节长度算出端点不返回未命中exists=false时不带 valuettl_ms/value_size_bytes 为 null。
var AppsCacheGet = common.Shortcut{
Service: appsService,
Command: "+cache-get",
Description: "Get a business cache key's value and metadata",
Risk: "read",
Tips: []string{
"Example: lark-cli apps +cache-get --app-id <app_id> --key spotbonus:2026:winners:list:v1",
"Example: lark-cli apps +cache-get --app-id <app_id> --environment online --key <key>",
},
Scopes: []string{"spark:app:read"},
AuthTypes: []string{"user"},
HasFormat: true,
Flags: []common.Flag{
{Name: "app-id", Desc: "Miaoda app id", Required: true},
{Name: "key", Desc: "business cache key", Required: true},
cacheEnvFlag(),
},
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
_, err := requireAppID(rctx.Str("app-id"))
return err
},
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
appID, _ := requireAppID(rctx.Str("app-id"))
return common.NewDryRunAPI().
GET(appCachePath(appID)).
Desc("Get a Miaoda app runtime cache key").
Params(dbEnvParams(rctx, map[string]interface{}{"key": rctx.Str("key")}))
},
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
appID, err := requireAppID(rctx.Str("app-id"))
if err != nil {
return err
}
key := rctx.Str("key")
data, err := rctx.CallAPITyped("GET", appCachePath(appID), dbEnvParams(rctx, map[string]interface{}{"key": key}), nil)
if err != nil {
return withAppsHint(err, appIDListHint)
}
out := projectCacheGet(data, key, rctx)
rctx.OutFormat(out, nil, func(w io.Writer) {
renderCacheGetPretty(w, out)
})
return nil
},
}
// projectCacheGet 组装 cache-get 输出key 回显、environment 取 resolved env、exists 直读;
// 命中时带 ttl_ms + value原始串+ value_size_bytesCLI 算),未命中时 ttl_ms/value_size_bytes 为 null、无 value。
func projectCacheGet(data map[string]interface{}, key string, rctx *common.RuntimeContext) map[string]interface{} {
exists := cacheBool(data["exists"])
out := map[string]interface{}{
"key": key,
"environment": resolvedEnv(data, rctx),
"exists": exists,
}
if exists {
val := common.GetString(data, "value")
out["ttl_ms"] = cacheInt(data["ttl_ms"])
out["value_size_bytes"] = len([]byte(val))
out["value"] = val
} else {
out["ttl_ms"] = nil
out["value_size_bytes"] = nil
}
return out
}
// renderCacheGetPretty 打元信息块key/environment/exists命中再加 ttl/value_size命中时末尾展开 value。
func renderCacheGetPretty(w io.Writer, out map[string]interface{}) {
exists, _ := out["exists"].(bool)
pairs := [][2]string{
{"key", common.GetString(out, "key")},
{"environment", common.GetString(out, "environment")},
{"exists", fmt.Sprintf("%v", exists)},
}
if exists {
pairs = append(pairs,
[2]string{"ttl", formatCacheTTL(out["ttl_ms"])},
[2]string{"value_size", humanBytes(out["value_size_bytes"])},
)
}
renderKeyValuePairs(w, pairs)
if exists {
fmt.Fprintln(w, "value:")
printCacheValuePretty(w, common.GetString(out, "value"))
}
}

View File

@@ -0,0 +1,357 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"encoding/json"
"strings"
"testing"
"github.com/larksuite/cli/internal/httpmock"
)
const (
cacheURL = "/open-apis/spark/v1/apps/app_x/cache"
cacheClearURL = "/open-apis/spark/v1/apps/app_x/cache/clear"
)
// cacheValueStr 是服务端在 wire 上透传的原始 JSON 字符串value 不反序列化)。
const cacheValueStr = `[{"name":"Alice","award":"Gold"},{"name":"Bob","award":"Silver"}]`
// ── cache-get ──
// TestAppsCacheGet_HitJSON命中时 json 默认——value 原样透传(不反序列化),
// value_size_bytes 由 CLI 按 value 字节长度算出environment 取服务端 resolved env。
func TestAppsCacheGet_HitJSON(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
"env": "online", "exists": true, "ttl_ms": 272000, "value": cacheValueStr,
}},
})
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "online", "--key", "k:1", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
d := parseEnvelopeData(t, stdout)
if d["key"] != "k:1" || d["environment"] != "online" || d["exists"] != true {
t.Fatalf("get hit data=%v", d)
}
if v, _ := d["value"].(string); v != cacheValueStr {
t.Fatalf("value must be raw passthrough string, got %v", d["value"])
}
if sz, _ := numericAsFloat(d["value_size_bytes"]); int(sz) != len(cacheValueStr) {
t.Fatalf("value_size_bytes = %v, want %d", d["value_size_bytes"], len(cacheValueStr))
}
// ttl_ms 必须是 JSON number透传服务端数字不得变成字符串JSON 解析后为 float64。
if _, ok := d["ttl_ms"].(float64); !ok {
t.Fatalf("ttl_ms must be a JSON number, got %T (%v)", d["ttl_ms"], d["ttl_ms"])
}
}
// TestAppsCacheGet_HitPrettypretty 把 value 反序列化后展开(含缩进后的字段),并打元信息标签。
func TestAppsCacheGet_HitPretty(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
"env": "online", "exists": true, "ttl_ms": 272000, "value": cacheValueStr,
}},
})
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "online", "--key", "k:1", "--format", "pretty", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
got := stdout.String()
for _, want := range []string{"key", "environment", "exists", "value", "Alice"} {
if !strings.Contains(got, want) {
t.Errorf("pretty missing %q:\n%s", want, got)
}
}
}
// TestAppsCacheGet_Miss未命中——exists=false无 valuettl_ms / value_size_bytes 为 null。
func TestAppsCacheGet_Miss(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
"env": "online", "exists": false,
}},
})
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "online", "--key", "k:1", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
d := parseEnvelopeData(t, stdout)
if d["exists"] != false {
t.Fatalf("miss exists=%v", d["exists"])
}
if _, ok := d["value"]; ok {
t.Fatalf("miss must not carry value: %v", d)
}
if d["ttl_ms"] != nil || d["value_size_bytes"] != nil {
t.Fatalf("miss ttl_ms/value_size_bytes must be null: %v", d)
}
}
// TestAppsCacheGet_ExistsAsString服务端把 exists 返成字符串 "true" 时仍按命中处理
// cacheBool 容错,防 exists 以字符串形态出现被误判成未命中、hit→miss 翻转)。
func TestAppsCacheGet_ExistsAsString(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
"env": "online", "exists": "true", "ttl_ms": 272000, "value": cacheValueStr,
}},
})
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "online", "--key", "k:1", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
d := parseEnvelopeData(t, stdout)
if d["exists"] != true {
t.Fatalf("exists string \"true\" 应按命中解析, got exists=%v", d["exists"])
}
if v, _ := d["value"].(string); v != cacheValueStr {
t.Fatalf("命中应带 value, got %v", d["value"])
}
}
// TestAppsCacheGet_PrettyNonJSONFallbackpretty 下 value 不是合法 JSON 时降级原样输出
// safeParseJSON 解析失败→原样打印,不报错、不吞值)。补齐 HitPretty 只覆盖了"能反序列化"路径的缺口。
func TestAppsCacheGet_PrettyNonJSONFallback(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
"env": "online", "exists": true, "ttl_ms": 272000, "value": "hello-plain-not-json",
}},
})
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "online", "--key", "k:1", "--format", "pretty", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
if !strings.Contains(stdout.String(), "hello-plain-not-json") {
t.Fatalf("非 JSON value 应原样输出(降级), got:\n%s", stdout.String())
}
}
// TestAppsCacheGet_TTLAsStringNormalized服务端把 ttl_ms 返成字符串 "272000" 时,
// 输出的 ttl_ms 必须归一成 JSON numbercacheInt不得随 wire 形态漂移成字符串。
func TestAppsCacheGet_TTLAsStringNormalized(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
"env": "online", "exists": true, "ttl_ms": "272000", "value": cacheValueStr,
}},
})
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "online", "--key", "k:1", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
d := parseEnvelopeData(t, stdout)
f, ok := d["ttl_ms"].(float64)
if !ok {
t.Fatalf("ttl_ms string wire 应归一成 JSON number, got %T (%v)", d["ttl_ms"], d["ttl_ms"])
}
if int(f) != 272000 {
t.Fatalf("ttl_ms = %v, want 272000", f)
}
}
// TestAppsCacheDelete_CountAsStringNormalized服务端把 deleted_key_count 返成字符串 "1" 时,
// 输出必须归一成 JSON numbercacheInt
func TestAppsCacheDelete_CountAsStringNormalized(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "DELETE", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"env": "dev", "deleted_key_count": "1"}},
})
if err := runAppsShortcut(t, AppsCacheDelete,
[]string{"+cache-delete", "--app-id", "app_x", "--environment", "dev", "--key", "k:1", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
d := parseEnvelopeData(t, stdout)
if _, ok := d["deleted_key_count"].(float64); !ok {
t.Fatalf("deleted_key_count string wire 应归一成 JSON number, got %T (%v)", d["deleted_key_count"], d["deleted_key_count"])
}
}
// TestAppsCacheGet_DryRunOmitsEnv不传 --environment 时 dry-run query 不带 env服务端自动选但带 key。
func TestAppsCacheGet_DryRunOmitsEnv(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--key", "k:1", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
a := firstDryRunAPI(t, stdout.String())
if a.Method != "GET" || a.URL != cacheURL {
t.Fatalf("dry-run = %s %s", a.Method, a.URL)
}
if _, ok := a.Params["env"]; ok {
t.Fatalf("no --environment → env must be omitted, params=%v", a.Params)
}
if a.Params["key"] != "k:1" {
t.Fatalf("key must be in query, params=%v", a.Params)
}
}
// TestAppsCacheGet_DryRunWithEnv显式 --environment dev → query 带 env=dev。
func TestAppsCacheGet_DryRunWithEnv(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--environment", "dev", "--key", "k:1", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
a := firstDryRunAPI(t, stdout.String())
if a.Params["env"] != "dev" {
t.Fatalf("env must be dev, params=%v", a.Params)
}
}
// TestAppsCacheGet_RequiresKey缺 --key → 校验错。
func TestAppsCacheGet_RequiresKey(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheGet,
[]string{"+cache-get", "--app-id", "app_x", "--as", "user"}, factory, stdout); err == nil {
t.Fatalf("expected required --key error")
}
}
// ── cache-delete ──
// TestAppsCacheDelete_Hit删中命中的 key → deleted_key_count=1pretty 打 "✓ cache deleted"。
func TestAppsCacheDelete_Hit(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "DELETE", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"env": "dev", "deleted_key_count": 1}},
})
if err := runAppsShortcut(t, AppsCacheDelete,
[]string{"+cache-delete", "--app-id", "app_x", "--environment", "dev", "--key", "k:1", "--format", "pretty", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
if !strings.Contains(stdout.String(), "✓ cache deleted") {
t.Fatalf("pretty: %s", stdout.String())
}
}
// TestAppsCacheDelete_AbsentJSON目标不存在 → 幂等成功deleted_key_count=0pretty 措辞区分。
func TestAppsCacheDelete_AbsentJSON(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "DELETE", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"env": "dev", "deleted_key_count": 0}},
})
if err := runAppsShortcut(t, AppsCacheDelete,
[]string{"+cache-delete", "--app-id", "app_x", "--environment", "dev", "--key", "k:1", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
d := parseEnvelopeData(t, stdout)
if sz, _ := numericAsFloat(d["deleted_key_count"]); int(sz) != 0 || d["key"] != "k:1" || d["environment"] != "dev" {
t.Fatalf("absent data=%v", d)
}
}
// TestAppsCacheDelete_AbsentPretty不存在 pretty 打 "✓ cache already absent"。
func TestAppsCacheDelete_AbsentPretty(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "DELETE", URL: cacheURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"env": "dev", "deleted_key_count": 0}},
})
if err := runAppsShortcut(t, AppsCacheDelete,
[]string{"+cache-delete", "--app-id", "app_x", "--environment", "dev", "--key", "k:1", "--format", "pretty", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
if !strings.Contains(stdout.String(), "already absent") {
t.Fatalf("pretty: %s", stdout.String())
}
}
// TestAppsCacheDelete_DryRunDELETE 方法、/cache 路由query 带 key + env。
func TestAppsCacheDelete_DryRun(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheDelete,
[]string{"+cache-delete", "--app-id", "app_x", "--environment", "dev", "--key", "k:1", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
a := firstDryRunAPI(t, stdout.String())
if a.Method != "DELETE" || a.URL != cacheURL {
t.Fatalf("dry-run = %s %s", a.Method, a.URL)
}
if a.Params["key"] != "k:1" || a.Params["env"] != "dev" {
t.Fatalf("params=%v", a.Params)
}
}
// ── cache-clear ──
// TestAppsCacheClear_Success清空成功 → deleted_key_count=128pretty 打 "✓ cache cleared: 128 entries (dev)"。
func TestAppsCacheClear_Success(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "POST", URL: cacheClearURL,
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"env": "dev", "deleted_key_count": 128}},
})
if err := runAppsShortcut(t, AppsCacheClear,
[]string{"+cache-clear", "--app-id", "app_x", "--environment", "dev", "--yes", "--format", "pretty", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
if !strings.Contains(stdout.String(), "✓ cache cleared: 128 entries (dev)") {
t.Fatalf("pretty: %s", stdout.String())
}
}
// TestAppsCacheClear_RequiresConfirmationhigh-risk-write 无 --yes → 被确认门拦截。
func TestAppsCacheClear_RequiresConfirmation(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheClear,
[]string{"+cache-clear", "--app-id", "app_x", "--environment", "dev", "--as", "user"}, factory, stdout); err == nil {
t.Fatalf("expected confirmation gate without --yes")
}
}
// TestAppsCacheClear_DryRunBodyWithEnvdry-run POST /cache/clearbody 带 env=dev。
func TestAppsCacheClear_DryRunBodyWithEnv(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheClear,
[]string{"+cache-clear", "--app-id", "app_x", "--environment", "dev", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
a := firstDryRunAPI(t, stdout.String())
if a.Method != "POST" || a.URL != cacheClearURL {
t.Fatalf("dry-run = %s %s", a.Method, a.URL)
}
if a.Body["env"] != "dev" {
t.Fatalf("body must carry env=dev, body=%v", a.Body)
}
}
// TestAppsCacheClear_DryRunBodyOmitsEnv不传 --environment → body 不带 env服务端自动选
func TestAppsCacheClear_DryRunBodyOmitsEnv(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsCacheClear,
[]string{"+cache-clear", "--app-id", "app_x", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
a := firstDryRunAPI(t, stdout.String())
if _, ok := a.Body["env"]; ok {
t.Fatalf("no --environment → body env must be omitted, body=%v", a.Body)
}
}
// firstDryRunAPI 解析 dry-run 输出的第一个 api[] 项method/url/params/body
// 复用本包规范的 dryRunAPIEnvelopeapi 现嵌在 data.api 下,见 dryrun_test.go
func firstDryRunAPI(t *testing.T, s string) dryRunAPICall {
t.Helper()
var env dryRunAPIEnvelope
if err := json.Unmarshal([]byte(s), &env); err != nil || len(env.API) == 0 {
t.Fatalf("bad dry-run json: %v\n%s", err, s)
}
return env.API[0]
}

View File

@@ -0,0 +1,99 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/larksuite/cli/internal/validate"
"github.com/larksuite/cli/shortcuts/common"
)
// 应用运行时缓存Cache调试命令共享件路由 + 环境 flag + 渲染。
//
// 三条命令都走 spark OpenAPI `/apps/{app_id}/cache[/clear]`按运行环境env→dbBranch隔离
// 环境 flag 用 cacheEnvFlag()(只 --environment不带 db 家族的旧名 --envenv 值经 dbEnv 读、
// 经 dbEnvParams 注入——get/delete 放 queryclear 放 body省略即服务端自动选分支
// appCachePath 返回缓存单 key 读/删 URLcacheGET 读、DELETE 删,靠方法区分)。
func appCachePath(appID string) string {
return fmt.Sprintf("%s/apps/%s/cache", apiBasePath, validate.EncodePathSegment(appID))
}
// appCacheClearPath 返回清空指定环境缓存 URLcache/clear。
func appCacheClearPath(appID string) string {
return fmt.Sprintf("%s/apps/%s/cache/clear", apiBasePath, validate.EncodePathSegment(appID))
}
// cacheEnvFlag 返回缓存命令的运行环境 flag。cache 是全新命令、从无旧名 --env
// 故只注册干净的 --environment不带 db 家族那套隐藏 --env + 拒收逻辑)。
// 省略即服务端按应用多环境状态自动选分支多环境→dev非多环境→online
func cacheEnvFlag() common.Flag {
return common.Flag{
Name: "environment",
Enum: []string{"dev", "online"},
Desc: "target runtime environment; leave unset to auto-select (multi-env app uses dev, single-env uses online), or pass dev/online",
}
}
// cacheBool 防御性解析布尔:真 bool 直接用;若服务端把 exists 返成字符串 "true"/"false" 也归一成 bool
// 其它类型按 false。避免 exists 万一以字符串形态出现时被误判成未命中hit→miss 翻转)。
func cacheBool(v interface{}) bool {
switch x := v.(type) {
case bool:
return x
case string:
return strings.EqualFold(strings.TrimSpace(x), "true")
}
return false
}
// cacheInt 把服务端下发的数值字段归一成 int64无法解析→nil。本仓惯例数值可能以字符串下发
// (见 numericAsFloat 的 string 分支),若直接透传,--format json 的字段类型会随服务端 wire 形态漂移
// number ↔ string。归一后输出类型恒定为数字或 null消费方无需自己容忍字符串。
func cacheInt(raw interface{}) interface{} {
if f, ok := numericAsFloat(raw); ok {
return int64(f)
}
return nil
}
// resolvedEnv 取服务端回吐的 resolved env缺失时兜底成请求侧 --environment可能为空
// 省略 --environment 时服务端自动选分支,靠服务端回吐才知道实际命中 dev / online。
func resolvedEnv(data map[string]interface{}, rctx *common.RuntimeContext) string {
if env := common.GetString(data, "env"); env != "" {
return env
}
return dbEnv(rctx)
}
// formatCacheTTL 把剩余 TTL毫秒格式化成 4m32s 这样的时长串;非数字返回 "—"。
func formatCacheTTL(ms interface{}) string {
f, ok := numericAsFloat(ms)
if !ok {
return "—"
}
return (time.Duration(int64(f)) * time.Millisecond).String()
}
// printCacheValuePretty 把 value 反序列化后缩进展开pretty 口径);非 JSON 则原样打印。
// 与「json 原样字符串、pretty 才反序列化」的设计一致。
func printCacheValuePretty(w io.Writer, raw string) {
v := safeParseJSON(raw)
if s, ok := v.(string); ok {
fmt.Fprintln(w, s)
return
}
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
fmt.Fprintln(w, raw)
return
}
w.Write(b)
fmt.Fprintln(w)
}

View File

@@ -64,6 +64,9 @@ func Shortcuts() []common.Shortcut {
AppsFileUpload,
AppsFileDelete,
AppsFileQuotaGet,
AppsCacheGet,
AppsCacheDelete,
AppsCacheClear,
AppsGitCredentialInit,
AppsGitCredentialList,
AppsGitCredentialRemove,

View File

@@ -20,13 +20,14 @@ import (
// - 3 git-credential
// - 5 sessioncreate/list/get/stop/chat+ 1 session-messages-list
// - 8 openapi-keylist/get/create/update/enable/disable/delete/reset
// - 3 cacheget/delete/clear
// - 3 plugininstall/uninstall/list
// - 6 automationlist/get/create/update/enable/disable
// - 9 rolerole CRUD + role-member list/add/remove + role-match-list= 79
func TestAppsShortcuts_Returns79(t *testing.T) {
// - 9 rolerole CRUD + role-member list/add/remove + role-match-list= 82
func TestAppsShortcuts_Returns82(t *testing.T) {
got := Shortcuts()
if len(got) != 79 {
t.Fatalf("Shortcuts() returned %d entries, want 79", len(got))
if len(got) != 82 {
t.Fatalf("Shortcuts() returned %d entries, want 82", len(got))
}
}

View File

@@ -4,6 +4,7 @@
package base
import (
"encoding/json"
"strings"
"testing"
@@ -250,7 +251,8 @@ func TestBaseFormQuestionsExecuteList(t *testing.T) {
"total": 2,
"questions": []interface{}{
map[string]interface{}{"id": "q_001", "title": "您的姓名", "required": true, "description": nil},
map[string]interface{}{"id": "q_002", "title": "您的年龄", "required": false, "description": nil},
map[string]interface{}{"id": "q_002", "title": "发票抬头", "required": false, "description": nil,
"visible_rule": map[string]interface{}{"logic": "and", "conditions": []interface{}{[]interface{}{"q_001", "==", "是"}}}},
},
},
},
@@ -258,9 +260,14 @@ func TestBaseFormQuestionsExecuteList(t *testing.T) {
if err := runShortcut(t, BaseFormQuestionsList, []string{"+form-questions-list", "--base-token", "app_x", "--table-id", "tbl_x", "--form-id", "vew_form1"}, factory, stdout); err != nil {
t.Fatalf("err=%v", err)
}
if got := stdout.String(); !strings.Contains(got, `"q_001"`) || !strings.Contains(got, `"total": 2`) {
got := stdout.String()
if !strings.Contains(got, `"q_001"`) || !strings.Contains(got, `"total": 2`) {
t.Fatalf("stdout=%s", got)
}
// The list output must forward visible_rule verbatim so agents can read existing display conditions.
if !strings.Contains(got, `"visible_rule"`) {
t.Fatalf("visible_rule missing from list output: %s", got)
}
}
func TestBaseFormQuestionsExecuteCreate(t *testing.T) {
@@ -296,11 +303,49 @@ func TestBaseFormQuestionsExecuteCreate(t *testing.T) {
t.Fatalf("expected error for invalid questions JSON")
}
})
t.Run("visible_rule passthrough", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
stub := &httpmock.Stub{
Method: "POST",
URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/forms/vew_form1/questions",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"questions": []interface{}{
map[string]interface{}{"id": "q_new1", "title": "发票抬头"},
},
},
},
}
reg.Register(stub)
args := []string{"+form-questions-create", "--base-token", "app_x", "--table-id", "tbl_x", "--form-id", "vew_form1",
"--questions", `[{"type":"text","title":"发票抬头","visible_rule":{"logic":"and","conditions":[["是否需要发票","==","是"]]}}]`}
if err := runShortcut(t, BaseFormQuestionsCreate, args, factory, stdout); err != nil {
t.Fatalf("err=%v", err)
}
var body struct {
Questions []map[string]interface{} `json:"questions"`
}
if err := json.Unmarshal(stub.CapturedBody, &body); err != nil {
t.Fatalf("captured body json err=%v body=%s", err, string(stub.CapturedBody))
}
if len(body.Questions) != 1 {
t.Fatalf("questions=%#v", body.Questions)
}
rule, ok := body.Questions[0]["visible_rule"].(map[string]interface{})
if !ok {
t.Fatalf("visible_rule not forwarded verbatim: body=%s", string(stub.CapturedBody))
}
if rule["logic"] != "and" {
t.Fatalf("visible_rule logic not preserved: %#v", rule)
}
})
}
func TestBaseFormQuestionsExecuteUpdate(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(&httpmock.Stub{
stub := &httpmock.Stub{
Method: "PATCH",
URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/forms/vew_form1/questions",
Body: map[string]interface{}{
@@ -311,15 +356,29 @@ func TestBaseFormQuestionsExecuteUpdate(t *testing.T) {
},
},
},
})
}
reg.Register(stub)
args := []string{"+form-questions-update", "--base-token", "app_x", "--table-id", "tbl_x", "--form-id", "vew_form1",
"--questions", `[{"id":"q_001","title":"更新后的问题","required":true}]`}
"--questions", `[{"id":"q_001","title":"更新后的问题","required":true,"visible_rule":{"logic":"and","conditions":[["q_002","==","是"]]}}]`}
if err := runShortcut(t, BaseFormQuestionsUpdate, args, factory, stdout); err != nil {
t.Fatalf("err=%v", err)
}
if got := stdout.String(); !strings.Contains(got, `"questions"`) || !strings.Contains(got, `"q_001"`) {
t.Fatalf("stdout=%s", got)
}
// visible_rule must be forwarded verbatim to the API (transcribe faithfully).
var body struct {
Questions []map[string]interface{} `json:"questions"`
}
if err := json.Unmarshal(stub.CapturedBody, &body); err != nil {
t.Fatalf("captured body json err=%v body=%s", err, string(stub.CapturedBody))
}
if len(body.Questions) != 1 {
t.Fatalf("questions=%#v", body.Questions)
}
if _, ok := body.Questions[0]["visible_rule"].(map[string]interface{}); !ok {
t.Fatalf("visible_rule not forwarded verbatim: body=%s", string(stub.CapturedBody))
}
}
func TestBaseFormQuestionsExecuteDelete(t *testing.T) {

View File

@@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/larksuite/cli/internal/output"
"github.com/larksuite/cli/shortcuts/common"
@@ -25,14 +26,25 @@ var BaseFormQuestionsCreate = common.Shortcut{
{Name: "base-token", Desc: "Base token (base_token)", Required: true},
{Name: "table-id", Desc: "table ID", Required: true},
{Name: "form-id", Desc: "form ID", Required: true},
{Name: "questions", Desc: `questions JSON array, max 10 items. Each item requires "title"(field title) and "type"(text/number/select/datetime/user/attachment/location). Optional fields: "description"(plain text or markdown link like [text](https://example.com)),"required","option_display_mode"(0=dropdown/1=vertical/2=horizontal,select only),"multiple"(bool,select/user),"options"([{"name":"opt","hue":"Blue"}],select only),"style"({"type":"plain/phone/url/email/barcode/rating","precision":2,"format":"yyyy/MM/dd","icon":"star","min":1,"max":5}). E.g. '[{"type":"text","title":"Your name","required":true}]'`, Required: true},
{Name: "questions", Desc: `questions JSON array, max 10 items. Each item requires "title"(field title) and "type"(text/number/select/datetime/user/attachment/location). Optional fields: "description"(plain text or markdown link like [text](https://example.com)),"required","option_display_mode"(0=dropdown/1=vertical/2=horizontal,select only),"multiple"(bool,select/user),"options"([{"name":"opt","hue":"Blue"}],select only),"style"({"type":"plain/phone/url/email/barcode/rating","precision":2,"format":"yyyy/MM/dd","icon":"star","min":1,"max":5}),"visible_rule"(display condition; same shape as view filter {"logic":"and","conditions":[["前序题目","==","是"]]}, field references another question's title/id, empty/absent = always shown). E.g. '[{"type":"text","title":"Your name","required":true}]'`, Required: true},
},
Tips: []string{
"If the form may already contain questions and has not been checked, run +form-questions-list for the same --base-token, --table-id, and --form-id. A verified empty form can create directly.",
"Each new question creates a field in the form's table; question IDs are field IDs.",
"Unless the user explicitly requests a separate same-title question, update an existing title with +form-questions-update instead of creating a duplicate.",
},
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
_, err := parseFormQuestionsCreate(runtime.Str("questions"))
return err
},
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
questions, _ := parseFormQuestionsCreate(runtime.Str("questions"))
return common.NewDryRunAPI().
POST("/open-apis/base/v3/bases/:base_token/tables/:table_id/forms/:form_id/questions").
Set("base_token", runtime.Str("base-token")).
Set("table_id", runtime.Str("table-id")).
Set("form_id", runtime.Str("form-id"))
Set("form_id", runtime.Str("form-id")).
Body(map[string]interface{}{"questions": questions})
},
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
baseToken := runtime.Str("base-token")
@@ -40,9 +52,9 @@ var BaseFormQuestionsCreate = common.Shortcut{
formId := runtime.Str("form-id")
questionsJSON := runtime.Str("questions")
var questions []interface{}
if err := json.Unmarshal([]byte(questionsJSON), &questions); err != nil {
return baseValidationErrorf("--questions must be a valid JSON array: %s", err)
questions, err := parseFormQuestionsCreate(questionsJSON)
if err != nil {
return err
}
data, err := baseV3Call(runtime, "POST",
@@ -71,3 +83,31 @@ var BaseFormQuestionsCreate = common.Shortcut{
return nil
},
}
func parseFormQuestionsCreate(raw string) ([]interface{}, error) {
var questions []interface{}
if err := json.Unmarshal([]byte(raw), &questions); err != nil {
return nil, baseValidationErrorf("--questions must be a valid JSON array: %s", err)
}
if questions == nil {
return nil, baseValidationErrorf("--questions must be a non-null JSON array")
}
if len(questions) > 10 {
return nil, baseValidationErrorf("--questions must contain at most 10 items")
}
for i, question := range questions {
item, ok := question.(map[string]interface{})
if !ok {
return nil, baseValidationErrorf("--questions item %d must be an object", i+1)
}
title, ok := item["title"].(string)
if !ok || strings.TrimSpace(title) == "" {
return nil, baseValidationErrorf("--questions item %d must include a non-empty string \"title\"", i+1)
}
questionType, ok := item["type"].(string)
if !ok || strings.TrimSpace(questionType) == "" {
return nil, baseValidationErrorf("--questions item %d must include a non-empty string \"type\"", i+1)
}
}
return questions, nil
}

View File

@@ -0,0 +1,24 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package base
import (
"strings"
"testing"
)
func TestBaseFormQuestionsCreateTipsRequireExistingQuestionCheck(t *testing.T) {
tips := strings.Join(BaseFormQuestionsCreate.Tips, "\n")
for _, want := range []string{
"+form-questions-list",
"verified empty form can create directly",
"question IDs are field IDs",
"explicitly requests a separate same-title question",
"+form-questions-update",
} {
if !strings.Contains(tips, want) {
t.Fatalf("tips missing %q:\n%s", want, tips)
}
}
}

View File

@@ -25,14 +25,26 @@ var BaseFormQuestionsUpdate = common.Shortcut{
{Name: "base-token", Desc: "Base token (base_token)", Required: true},
{Name: "table-id", Desc: "table ID", Required: true},
{Name: "form-id", Desc: "form ID", Required: true},
{Name: "questions", Desc: `questions JSON array, max 10 items, each item must include "id". Supported fields: "id"(required),"title","description"(plain text or markdown link like [text](https://example.com)),"required","option_display_mode"(0=dropdown,1=vertical,2=horizontal,select only). E.g. '[{"id":"q_001","title":"Updated?","required":true}]'`, Required: true},
{Name: "questions", Desc: `questions JSON array, max 10 items, each item must include "id". Update uses full question overwrite semantics: omitted/empty fields are written as defaults/empty, so run +form-questions-list first and include existing values you want to keep. Supported fields: "id"(required),"title","description"(plain text or markdown link like [text](https://example.com)),"required","option_display_mode"(0=dropdown,1=vertical,2=horizontal,select only),"visible_rule"(display condition; same shape as view filter {"logic":"and","conditions":[["前序题目","==","是"]]}, field references another question's title/id; pass null or omit to clear). E.g. '[{"id":"q_001","title":"Updated?","required":true}]'`, Required: true},
},
Tips: []string{
"Update uses full question overwrite semantics, not a patch.",
"Run +form-questions-list first and include existing title/description/required/option_display_mode/visible_rule values you want to keep.",
"Omitted fields reset to defaults; empty strings, null, and empty arrays are written as empty/clear when accepted by the API.",
},
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
return common.NewDryRunAPI().
api := common.NewDryRunAPI().
PATCH("/open-apis/base/v3/bases/:base_token/tables/:table_id/forms/:form_id/questions").
Set("base_token", runtime.Str("base-token")).
Set("table_id", runtime.Str("table-id")).
Set("form_id", runtime.Str("form-id"))
// Transcribe the questions body verbatim so the preview shows exactly
// what would be sent (including optional fields like visible_rule).
var questions []interface{}
if err := json.Unmarshal([]byte(runtime.Str("questions")), &questions); err == nil {
api.Body(map[string]interface{}{"questions": questions})
}
return api
},
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
baseToken := runtime.Str("base-token")

View File

@@ -29,6 +29,7 @@ var BaseURLResolve = common.Shortcut{
Risk: "read",
Scopes: []string{},
ConditionalScopes: []string{
"base:block:read",
"base:field:read",
"base:record:read",
"wiki:node:retrieve",
@@ -40,7 +41,7 @@ var BaseURLResolve = common.Shortcut{
{Name: "query", Hidden: true, Desc: "Alias for --url; accepted to recover from AI routing mistakes"},
},
Tips: []string{
`Example: lark-cli base +url-resolve --url "https://example.larkoffice.com/base/<base_token>?table=<table_id>&view=<view_id>"`,
`Example: lark-cli base +url-resolve --url "https://example.larkoffice.com/base/<base_token>?table=<block_id>&view=<view_id>"`,
"Only URLs are accepted. For Base titles or keywords, use +title-resolve --title.",
},
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
@@ -57,10 +58,34 @@ var BaseURLResolve = common.Shortcut{
return common.NewDryRunAPI().Set("error", err.Error())
}
switch classifyBaseURL(parsed) {
case "base_url":
baseToken := firstPathSegmentAfter(parsed.Path, "/base/")
if selectedBlockID := strings.TrimSpace(parsed.Query().Get("table")); selectedBlockID != "" {
return common.NewDryRunAPI().
POST("/open-apis/base/v3/bases/:base_token/blocks/list").
Body(map[string]interface{}{}).
Set("base_token", baseToken).
Set("selected_block_id", selectedBlockID)
}
return common.NewDryRunAPI().Set("url", raw).Set("resolution", "local")
case "wiki_url":
return common.NewDryRunAPI().
GET("/open-apis/wiki/v2/spaces/get_node").
dry := common.NewDryRunAPI()
selectedBlockID := strings.TrimSpace(parsed.Query().Get("table"))
if selectedBlockID == "" {
return dry.
GET("/open-apis/wiki/v2/spaces/get_node").
Params(map[string]interface{}{"token": firstPathSegmentAfter(parsed.Path, "/wiki/")})
}
dry.Desc("2-step: resolve the Wiki node to a Base, then identify the selected Base block")
dry.GET("/open-apis/wiki/v2/spaces/get_node").
Desc("[1] Resolve the Wiki node to its underlying Base").
Params(map[string]interface{}{"token": firstPathSegmentAfter(parsed.Path, "/wiki/")})
dry.POST("/open-apis/base/v3/bases/:base_token/blocks/list").
Desc("[2] List Base blocks and match selected_block_id").
Body(map[string]interface{}{})
return dry.
Set("base_token", "<obj_token from step 1>").
Set("selected_block_id", selectedBlockID)
case "record_share_url":
return common.NewDryRunAPI().
GET("/open-apis/base/v3/record_share/:record_share_token/meta").
@@ -170,7 +195,7 @@ func executeBaseURLResolve(runtime *common.RuntimeContext) error {
switch classifyBaseURL(parsed) {
case "base_url":
out := resolveBaseURL(parsed)
enrichBaseResolveHint(runtime, out)
enrichBaseResolveHint(runtime, out, resolveBaseURLSelection(parsed))
runtime.OutFormat(out, nil, nil)
return nil
case "wiki_url":
@@ -178,6 +203,9 @@ func executeBaseURLResolve(runtime *common.RuntimeContext) error {
if err != nil {
return err
}
selection := resolveBaseURLSelection(parsed)
applyBaseURLSelection(out, selection)
enrichBaseResolveHint(runtime, out, selection)
runtime.OutFormat(out, nil, nil)
return nil
case "record_share_url":
@@ -251,24 +279,50 @@ func classifyBaseURL(u *url.URL) string {
}
func resolveBaseURL(u *url.URL) map[string]interface{} {
query := u.Query()
out := map[string]interface{}{
"input_type": "base_url",
"resource_type": "bitable",
"base_token": firstPathSegmentAfter(u.Path, "/base/"),
}
if tableID := strings.TrimSpace(query.Get("table")); tableID != "" {
out["table_id"] = tableID
}
if viewID := strings.TrimSpace(query.Get("view")); viewID != "" {
out["view_id"] = viewID
}
if recordID := strings.TrimSpace(query.Get("record")); recordID != "" {
out["record_id"] = recordID
}
applyBaseURLSelection(out, resolveBaseURLSelection(u))
return out
}
type baseURLSelection struct {
blockID string
viewID string
recordID string
}
func resolveBaseURLSelection(u *url.URL) baseURLSelection {
query := u.Query()
return baseURLSelection{
blockID: strings.TrimSpace(query.Get("table")),
viewID: strings.TrimSpace(query.Get("view")),
recordID: strings.TrimSpace(query.Get("record")),
}
}
func applyBaseURLSelection(out map[string]interface{}, selection baseURLSelection) {
if selection.blockID != "" {
// The Base web UI historically uses the query key "table" for the
// currently selected top-level block. Its value can identify a table,
// dashboard, workflow, or another block type. Keep it neutral until the
// block directory confirms the resource type.
out["block_id"] = selection.blockID
out["selection_source"] = "url_query"
}
}
func applyResolvedTableSelection(out map[string]interface{}, selection baseURLSelection) {
if selection.viewID != "" {
out["view_id"] = selection.viewID
}
if selection.recordID != "" {
out["record_id"] = selection.recordID
}
}
func resolveWikiBaseURL(runtime *common.RuntimeContext, u *url.URL) (map[string]interface{}, error) {
token := firstPathSegmentAfter(u.Path, "/wiki/")
data, err := runtime.CallAPITyped("GET", "/open-apis/wiki/v2/spaces/get_node", map[string]interface{}{"token": token}, nil)
@@ -368,13 +422,89 @@ func executeBaseTitleResolve(runtime *common.RuntimeContext) error {
}
}
func enrichBaseResolveHint(runtime *common.RuntimeContext, out map[string]interface{}) {
func enrichBaseResolveHint(runtime *common.RuntimeContext, out map[string]interface{}, selection baseURLSelection) {
baseToken := strings.TrimSpace(common.GetString(out, "base_token"))
tableID := strings.TrimSpace(common.GetString(out, "table_id"))
if baseToken == "" || tableID == "" {
selectedBlockID := strings.TrimSpace(common.GetString(out, "block_id"))
if baseToken == "" || selectedBlockID == "" {
out["hint"] = resolveHint("", nil)
return
}
if block, found, err := resolveSelectedBaseBlock(runtime, baseToken, selectedBlockID); err == nil && found {
out["block_type"] = block.Type
if block.Name != "" {
out["block_name"] = block.Name
}
switch block.Type {
case "table":
applyResolvedTableSelection(out, selection)
enrichResolvedTable(runtime, out, baseToken, selectedBlockID)
case "dashboard":
out["dashboard_id"] = selectedBlockID
out["hint"] = map[string]interface{}{
"next_step": "this dashboard is only the block currently selected by the URL; if the user names a different dashboard than block_name, use +dashboard-list and match that name first, otherwise use +dashboard-get to inspect this dashboard",
}
case "workflow":
out["workflow_id"] = selectedBlockID
out["hint"] = map[string]interface{}{
"next_step": "use +workflow-get to inspect the resolved workflow",
}
case "folder":
out["hint"] = map[string]interface{}{
"next_step": fmt.Sprintf("use +base-block-list --base-token %s --parent-id %s to list this folder's direct children", baseToken, selectedBlockID),
}
case "docx":
if block.DocxToken != "" {
out["docx_token"] = block.DocxToken
out["hint"] = map[string]interface{}{
"next_step": fmt.Sprintf("use docs +fetch --doc %s to read this document", block.DocxToken),
}
} else {
out["hint"] = map[string]interface{}{
"next_step": "use +base-block-list --type docx and match block_id to retrieve this document's docx_token",
}
}
default:
out["hint"] = resolveUnknownBlockHint()
}
return
}
out["hint"] = resolveUnknownBlockHint()
}
type resolvedBaseBlock struct {
ID string
Type string
Name string
DocxToken string
}
func resolveSelectedBaseBlock(runtime *common.RuntimeContext, baseToken, selectedBlockID string) (resolvedBaseBlock, bool, error) {
data, err := baseV3Call(runtime, "POST", baseV3Path("bases", baseToken, "blocks", "list"), nil, map[string]interface{}{})
if err != nil {
return resolvedBaseBlock{}, false, err
}
for _, item := range common.GetSlice(data, "blocks") {
row, ok := item.(map[string]interface{})
if !ok {
continue
}
block := resolvedBaseBlock{
ID: strings.TrimSpace(common.GetString(row, "id")),
Type: strings.TrimSpace(common.GetString(row, "type")),
Name: strings.TrimSpace(common.GetString(row, "name")),
DocxToken: strings.TrimSpace(common.GetString(row, "docx_token")),
}
if block.ID == selectedBlockID {
return block, true, nil
}
}
return resolvedBaseBlock{}, false, nil
}
func enrichResolvedTable(runtime *common.RuntimeContext, out map[string]interface{}, baseToken, tableID string) {
out["table_id"] = tableID
fields, total, err := listAllFields(runtime, baseToken, tableID, 0, 100)
if err != nil {
out["hint"] = resolveHint(tableID, nil)
@@ -383,6 +513,12 @@ func enrichBaseResolveHint(runtime *common.RuntimeContext, out map[string]interf
out["hint"] = resolveHint(tableID, map[string]interface{}{"fields": map[string]interface{}{"fields": fields, "total": total}})
}
func resolveUnknownBlockHint() map[string]interface{} {
return map[string]interface{}{
"next_step": "use +base-block-list and match block_id to determine whether this is a table, dashboard, workflow, folder, or docx block",
}
}
func enrichRecordShareResolveHint(runtime *common.RuntimeContext, out map[string]interface{}) {
baseToken := strings.TrimSpace(common.GetString(out, "base_token"))
tableID := strings.TrimSpace(common.GetString(out, "table_id"))

View File

@@ -4,6 +4,7 @@
package base
import (
"net/http"
"strings"
"testing"
@@ -17,6 +18,9 @@ import (
func TestBaseURLResolveBaseURL(t *testing.T) {
t.Run("with coordinates", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "tbl123", "type": "table", "name": "Orders"},
))
reg.Register(fieldListStub("bas123", "tbl123"))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve",
@@ -31,7 +35,7 @@ func TestBaseURLResolveBaseURL(t *testing.T) {
if data["input_type"] != "base_url" || data["base_token"] != "bas123" {
t.Fatalf("unexpected output: %#v", data)
}
if data["table_id"] != "tbl123" || data["view_id"] != "vew123" || data["record_id"] != "rec123" {
if data["block_id"] != "tbl123" || data["selection_source"] != "url_query" || data["block_type"] != "table" || data["table_id"] != "tbl123" || data["view_id"] != "vew123" || data["record_id"] != "rec123" {
t.Fatalf("missing Base coordinates: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
@@ -62,45 +66,213 @@ func TestBaseURLResolveBaseURL(t *testing.T) {
}
})
t.Run("field list enrichment failure still returns coordinates", func(t *testing.T) {
t.Run("unconfirmed selected block stays neutral", func(t *testing.T) {
factory, stdout, _ := newExecuteFactory(t)
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=tbl123", "--as", "user",
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=tbl123&view=vew_stale&record=rec_stale", "--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["base_token"] != "bas123" || data["table_id"] != "tbl123" {
if data["base_token"] != "bas123" || data["block_id"] != "tbl123" {
t.Fatalf("unexpected output: %#v", data)
}
if _, ok := data["table_id"]; ok {
t.Fatalf("unconfirmed block must not be reported as a table: %#v", data)
}
if _, ok := data["view_id"]; ok {
t.Fatalf("unconfirmed block must not expose table-only view_id: %#v", data)
}
if _, ok := data["record_id"]; ok {
t.Fatalf("unconfirmed block must not expose table-only record_id: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
if hint["next_step"] != nextStepRecordList {
if !strings.Contains(hint["next_step"].(string), "+base-block-list") {
t.Fatalf("unexpected hint: %#v", hint)
}
if _, ok := hint["fields"]; ok {
t.Fatalf("fields should be omitted when enrichment fails: %#v", hint)
}
})
t.Run("field endpoint does not confirm untyped block", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "tbl_other", "type": "table", "name": "Other"},
))
fieldStub := fieldListStub("bas123", "tbl123")
fieldStub.Optional = true
fieldStub.OnMatch = func(_ *http.Request) {
t.Fatalf("field endpoint must not be used to infer selected block type")
}
reg.Register(fieldStub)
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=tbl123&view=vew_stale", "--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["block_id"] != "tbl123" {
t.Fatalf("unexpected block coordinates: %#v", data)
}
if _, ok := data["block_type"]; ok {
t.Fatalf("field endpoint must not confirm block type without block directory: %#v", data)
}
if _, ok := data["table_id"]; ok {
t.Fatalf("field endpoint must not promote an untyped block to table_id: %#v", data)
}
if _, ok := data["view_id"]; ok {
t.Fatalf("untyped block must not expose table-only view_id: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
if _, ok := hint["fields"]; ok {
t.Fatalf("fields should be omitted when block type is unconfirmed: %#v", hint)
}
if !strings.Contains(hint["next_step"].(string), "+base-block-list") {
t.Fatalf("unexpected hint: %#v", hint)
}
})
t.Run("dashboard selected through table query key", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "blk_dashboard", "type": "dashboard", "name": "Sales"},
))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=blk_dashboard&view=vew_stale&record=rec_stale", "--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["block_id"] != "blk_dashboard" || data["selection_source"] != "url_query" || data["block_type"] != "dashboard" || data["dashboard_id"] != "blk_dashboard" || data["block_name"] != "Sales" {
t.Fatalf("unexpected dashboard coordinates: %#v", data)
}
if _, ok := data["table_id"]; ok {
t.Fatalf("dashboard must not be reported as table_id: %#v", data)
}
if _, ok := data["view_id"]; ok {
t.Fatalf("dashboard must not expose table-only view_id: %#v", data)
}
if _, ok := data["record_id"]; ok {
t.Fatalf("dashboard must not expose table-only record_id: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
nextStep := hint["next_step"].(string)
if !strings.Contains(nextStep, "+dashboard-get") || !strings.Contains(nextStep, "+dashboard-list") || !strings.Contains(nextStep, "different dashboard than block_name") {
t.Fatalf("unexpected dashboard hint: %#v", hint)
}
})
t.Run("workflow selected through table query key", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "wkf_notify", "type": "workflow", "name": "Notify"},
))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=wkf_notify&view=vew_stale&record=rec_stale", "--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["block_id"] != "wkf_notify" || data["block_type"] != "workflow" || data["workflow_id"] != "wkf_notify" {
t.Fatalf("unexpected workflow coordinates: %#v", data)
}
if _, ok := data["table_id"]; ok {
t.Fatalf("workflow must not be reported as table_id: %#v", data)
}
if _, ok := data["view_id"]; ok {
t.Fatalf("workflow must not expose table-only view_id: %#v", data)
}
if _, ok := data["record_id"]; ok {
t.Fatalf("workflow must not expose table-only record_id: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
if !strings.Contains(hint["next_step"].(string), "+workflow-get") {
t.Fatalf("unexpected workflow hint: %#v", hint)
}
})
t.Run("folder selected through table query key", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "bfl_projects", "type": "folder", "name": "Projects"},
))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=bfl_projects&view=vew_stale&record=rec_stale", "--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["block_id"] != "bfl_projects" || data["block_type"] != "folder" || data["block_name"] != "Projects" {
t.Fatalf("unexpected folder coordinates: %#v", data)
}
if _, ok := data["table_id"]; ok {
t.Fatalf("folder must not be reported as table_id: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
nextStep := hint["next_step"].(string)
if !strings.Contains(nextStep, "+base-block-list --base-token bas123 --parent-id bfl_projects") || strings.Contains(nextStep, "determine whether") {
t.Fatalf("unexpected folder hint: %#v", hint)
}
})
t.Run("docx selected through table query key", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "blk_doc", "type": "docx", "name": "Spec", "docx_token": "docx123"},
))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/base/bas123?table=blk_doc&view=vew_stale&record=rec_stale", "--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["block_id"] != "blk_doc" || data["block_type"] != "docx" || data["block_name"] != "Spec" || data["docx_token"] != "docx123" {
t.Fatalf("unexpected docx coordinates: %#v", data)
}
if _, ok := data["table_id"]; ok {
t.Fatalf("docx must not be reported as table_id: %#v", data)
}
hint, _ := data["hint"].(map[string]interface{})
nextStep := hint["next_step"].(string)
if !strings.Contains(nextStep, "docs +fetch --doc docx123") || strings.Contains(nextStep, "determine whether") {
t.Fatalf("unexpected docx hint: %#v", hint)
}
})
}
func baseBlockListResolveStub(baseToken string, blocks ...map[string]interface{}) *httpmock.Stub {
items := make([]interface{}, 0, len(blocks))
for _, block := range blocks {
items = append(items, block)
}
return &httpmock.Stub{
Method: "POST",
URL: "/open-apis/base/v3/bases/" + baseToken + "/blocks/list",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"blocks": items,
"total": len(items),
},
},
}
}
func TestBaseURLResolveWikiURL(t *testing.T) {
t.Run("bitable", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "GET",
URL: "/open-apis/wiki/v2/spaces/get_node?token=wik123",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"node": map[string]interface{}{
"obj_type": "bitable",
"obj_token": "bas123",
"title": "Demo Base",
},
},
},
})
reg.Register(wikiBaseNodeStub("wik123", "bas123", "Demo Base"))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve", "--url", "https://example.larkoffice.com/wiki/wik123", "--as", "user",
@@ -114,6 +286,57 @@ func TestBaseURLResolveWikiURL(t *testing.T) {
}
})
t.Run("bitable with table coordinates", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(wikiBaseNodeStub("wik123", "bas123", "Demo Base"))
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "tbl123", "type": "table", "name": "Orders"},
))
reg.Register(fieldListStub("bas123", "tbl123"))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve",
"--url", "https://example.larkoffice.com/wiki/wik123?table=tbl123&view=vew123&record=rec123",
"--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["input_type"] != "wiki_url" || data["base_token"] != "bas123" || data["block_id"] != "tbl123" || data["block_type"] != "table" || data["table_id"] != "tbl123" || data["view_id"] != "vew123" || data["record_id"] != "rec123" {
t.Fatalf("unexpected Wiki Base table coordinates: %#v", data)
}
})
t.Run("bitable with dashboard selection", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(wikiBaseNodeStub("wik123", "bas123", "Demo Base"))
reg.Register(baseBlockListResolveStub("bas123",
map[string]interface{}{"id": "blk_dashboard", "type": "dashboard", "name": "Sales"},
))
err := runShortcutWithAuthTypes(t, BaseURLResolve, authTypes(), []string{
"+url-resolve",
"--url", "https://example.larkoffice.com/wiki/wik123?table=blk_dashboard&view=vew_stale&record=rec_stale",
"--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("err=%v", err)
}
data := decodeBaseEnvelope(t, stdout)
if data["input_type"] != "wiki_url" || data["block_id"] != "blk_dashboard" || data["block_type"] != "dashboard" || data["dashboard_id"] != "blk_dashboard" {
t.Fatalf("unexpected Wiki Base dashboard coordinates: %#v", data)
}
if _, ok := data["view_id"]; ok {
t.Fatalf("dashboard must not expose table-only view_id: %#v", data)
}
if _, ok := data["record_id"]; ok {
t.Fatalf("dashboard must not expose table-only record_id: %#v", data)
}
})
t.Run("non bitable", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
reg.Register(&httpmock.Stub{
@@ -136,6 +359,23 @@ func TestBaseURLResolveWikiURL(t *testing.T) {
})
}
func wikiBaseNodeStub(wikiToken, baseToken, title string) *httpmock.Stub {
return &httpmock.Stub{
Method: "GET",
URL: "/open-apis/wiki/v2/spaces/get_node?token=" + wikiToken,
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"node": map[string]interface{}{
"obj_type": "bitable",
"obj_token": baseToken,
"title": title,
},
},
},
}
}
func TestBaseURLResolveRecordShareURL(t *testing.T) {
t.Run("enriched", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)

View File

@@ -783,6 +783,20 @@ func TestBaseJSONExamplesLiveInFlagDescriptions(t *testing.T) {
`JSON array of question IDs to delete, max 10 items, e.g. '["q_001","q_002"]'`,
},
},
{
name: "form question create visible_rule",
shortcut: BaseFormQuestionsCreate,
wantHelp: []string{
`"visible_rule"(display condition; same shape as view filter`,
},
},
{
name: "form question update visible_rule",
shortcut: BaseFormQuestionsUpdate,
wantHelp: []string{
`"visible_rule"(display condition; same shape as view filter`,
},
},
{
name: "record search json",
shortcut: BaseRecordSearch,
@@ -1028,6 +1042,39 @@ func TestBaseFieldUpdateHelpGuidesAgents(t *testing.T) {
}
}
func TestBaseFormQuestionsUpdateHelpGuidesFullOverwrite(t *testing.T) {
parent := &cobra.Command{Use: "base"}
BaseFormQuestionsUpdate.Mount(parent, &cmdutil.Factory{})
cmd := parent.Commands()[0]
help := cmd.Flags().FlagUsages()
wantHelp := []string{
"Update uses full question overwrite semantics",
"run +form-questions-list first",
"include existing values you want to keep",
"pass null or omit to clear",
}
for _, want := range wantHelp {
if !strings.Contains(help, want) {
t.Fatalf("flag help missing %q:\n%s", want, help)
}
}
tips := strings.Join(cmdutil.GetTips(cmd), "\n")
wantTips := []string{
"full question overwrite semantics, not a patch",
"Run +form-questions-list first",
"title/description/required/option_display_mode/visible_rule",
"Omitted fields reset to defaults",
"empty strings, null, and empty arrays are written as empty/clear",
}
for _, want := range wantTips {
if !strings.Contains(tips, want) {
t.Fatalf("tips missing %q:\n%s", want, tips)
}
}
}
func TestBaseAttachmentHelpGuidesAgents(t *testing.T) {
tests := []struct {
name string

View File

@@ -250,7 +250,7 @@ var CalendarAgenda = common.Shortcut{
}
}
backfillDescriptionRich(e)
collapseDescription(e)
filtered = append(filtered, e)
}

View File

@@ -32,8 +32,8 @@ func buildEventData(runtime *common.RuntimeContext, startTs, endTs string) map[s
if rrule := runtime.Str("rrule"); rrule != "" {
eventData["recurrence"] = rrule
}
if descriptionRich := descriptionRichToSend(runtime); descriptionRich != "" {
eventData["description_rich"] = descriptionRich
if description := descriptionToSend(runtime); description != "" {
eventData["description_rich"] = description
}
return eventData
}
@@ -120,8 +120,7 @@ var CalendarCreate = common.Shortcut{
{Name: "summary", Desc: "event title"},
{Name: "start", Desc: "start time (ISO 8601)", Required: true},
{Name: "end", Desc: "end time (ISO 8601)", Required: true},
{Name: "description", Desc: "deprecated: plain-text description; use --description-rich (Markdown) instead", Hidden: true},
{Name: "description-rich", Desc: "event description as Markdown (@file or - for stdin); the unified description field. Supports bold/italic/underline/strikethrough, links, headings (`#`..`###`), blockquotes (`>`), ordered/unordered lists, horizontal rules (`---`), GFM tables, and images (`![name](url)`; a remote URL is used as-is, and a local image path relative to and inside the current working directory is auto-uploaded to Lark drive and rendered inline — absolute/out-of-cwd paths are rejected). A Lark doc URL (bare or as a Markdown link) is auto-resolved to an inline doc-mention chip showing its title. Inside a GFM table cell, stack multiple lines with `<br>`; each line may itself be an ordered/unordered list item, image or styled text (e.g. `1. a<br>2. b`, `- x<br>- y`, `![p](url)<br>**bold**`).", Input: []string{common.File, common.Stdin}},
{Name: "description", Desc: "event description as Markdown (@file or - for stdin); the unified description field. Supports bold/italic/underline/strikethrough, links, headings (`#`..`###`), blockquotes (`>`), ordered/unordered lists, horizontal rules (`---`), GFM tables, and images (`![name](url)`; a remote URL is used as-is, and a local image path relative to and inside the current working directory is auto-uploaded to Lark drive and rendered inline — absolute/out-of-cwd paths are rejected). A Lark doc URL (bare or as a Markdown link) is auto-resolved to an inline doc-mention chip showing its title. Inside a GFM table cell, stack multiple lines with `<br>`; each line may itself be an ordered/unordered list item, image or styled text (e.g. `1. a<br>2. b`, `- x<br>- y`, `![p](url)<br>**bold**`).", Input: []string{common.File, common.Stdin}},
{Name: "attendee-ids", Desc: "attendee IDs, comma-separated (supports user ou_, chat oc_, room omm_)"},
{Name: "calendar-id", Desc: "calendar ID (default: primary)"},
{Name: "rrule", Desc: "recurrence rule (rfc5545)"},
@@ -234,7 +233,7 @@ var CalendarCreate = common.Shortcut{
if err != nil {
return errs.NewValidationError(errs.SubtypeInvalidArgument, "--end: %v", err).WithParam("--end")
}
if err := resolveDescriptionRichImages(runtime, calendarId); err != nil {
if err := resolveDescriptionImages(runtime, calendarId); err != nil {
return err
}

View File

@@ -170,7 +170,7 @@ func buildCalendarEventOutput(event *calendarEvent) (map[string]interface{}, err
if status, _ := out["status"].(string); status != "cancelled" {
delete(out, "status")
}
backfillDescriptionRich(out)
collapseDescription(out)
return out, nil
}

View File

@@ -988,8 +988,9 @@ func TestUpdate_PatchEventOnly(t *testing.T) {
if err := json.Unmarshal(stub.CapturedBody, &body); err != nil {
t.Fatalf("unmarshal captured patch body: %v", err)
}
// The deprecated, hidden --description folds into description_rich; the CLI
// never sends the plain description field (mutually exclusive downstream).
// --description is the unified field, treated as rich text and sent as
// description_rich; the CLI never sends the plain description field
// (mutually exclusive downstream).
if body["summary"] != "Updated Meeting" || body["description_rich"] != "Updated description" {
t.Fatalf("unexpected patch body: %#v", body)
}
@@ -1411,18 +1412,17 @@ func TestAgenda_UnifiesDescriptionRich(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
out := stdout.String()
// Read keeps both fields: description (plain) and description_rich (rich).
if !strings.Contains(out, "\"description\": \"[测试]\\n友情提醒\"") {
t.Errorf("expected plain description retained, got: %s", out)
}
if !strings.Contains(out, "\"description_rich\": \"友情提醒\"") {
t.Errorf("expected rich description surfaced, got: %s", out)
// Read exposes a single unified description field: it carries the rich
// (Markdown) value when present, and the plain text otherwise. The internal
// description_rich key is never surfaced.
if !strings.Contains(out, "\"description\": \"友情提醒\"") {
t.Errorf("expected rich value surfaced under description, got: %s", out)
}
if !strings.Contains(out, "\"description\": \"just text\"") {
t.Errorf("expected plain description retained for plain-only event, got: %s", out)
t.Errorf("expected plain description surfaced for plain-only event, got: %s", out)
}
if !strings.Contains(out, "\"description_rich\": \"just text\"") {
t.Errorf("expected description_rich backfilled from plain, got: %s", out)
if strings.Contains(out, "description_rich") {
t.Errorf("description_rich must not appear in output, got: %s", out)
}
}
@@ -3438,7 +3438,8 @@ func TestGet_Success_FlattensAndConvertsTimes(t *testing.T) {
}
func TestGet_UnifiesDescriptionRich(t *testing.T) {
// Read keeps both description (plain) and description_rich (rich).
// Read exposes a single unified description field carrying the rich value
// when present, and the plain text otherwise; description_rich is dropped.
t.Run("rich present", func(t *testing.T) {
f, stdout, _, reg := cmdutil.TestFactory(t, defaultConfig())
reg.Register(&httpmock.Stub{
@@ -3462,17 +3463,16 @@ func TestGet_UnifiesDescriptionRich(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
out := stdout.String()
if !strings.Contains(out, "\"description\": \"[表格]\"") {
t.Errorf("expected plain description retained, got: %s", out)
if !strings.Contains(out, "| a | b |") {
t.Errorf("expected rich value surfaced under description, got: %s", out)
}
if !strings.Contains(out, "\"description_rich\":") {
t.Errorf("expected description_rich in output, got: %s", out)
if strings.Contains(out, "description_rich") {
t.Errorf("description_rich must not appear in output, got: %s", out)
}
})
// When only a plain description exists, description_rich is backfilled from it
// and the plain description is still returned.
t.Run("only plain backfills rich", func(t *testing.T) {
// When only a plain description exists, it is surfaced under description.
t.Run("only plain surfaces under description", func(t *testing.T) {
f, stdout, _, reg := cmdutil.TestFactory(t, defaultConfig())
reg.Register(&httpmock.Stub{
Method: "GET",
@@ -3495,10 +3495,10 @@ func TestGet_UnifiesDescriptionRich(t *testing.T) {
}
out := stdout.String()
if !strings.Contains(out, "\"description\": \"just text\"") {
t.Errorf("expected plain description retained, got: %s", out)
t.Errorf("expected plain description surfaced, got: %s", out)
}
if !strings.Contains(out, "\"description_rich\": \"just text\"") {
t.Errorf("expected description_rich backfilled from plain, got: %s", out)
if strings.Contains(out, "description_rich") {
t.Errorf("description_rich must not appear in output, got: %s", out)
}
})
}

View File

@@ -29,8 +29,7 @@ var CalendarUpdate = common.Shortcut{
{Name: "event-id", Desc: "event ID to update", Required: true},
{Name: "calendar-id", Desc: "calendar ID (default: primary)"},
{Name: "summary", Desc: "event title"},
{Name: "description", Desc: "deprecated: plain-text description; use --description-rich (Markdown) instead", Hidden: true},
{Name: "description-rich", Desc: "event description as Markdown (@file or - for stdin); the unified description field. Supports bold/italic/underline/strikethrough, links, headings (`#`..`###`), blockquotes (`>`), ordered/unordered lists, horizontal rules (`---`), GFM tables, and images (`![name](url)`; a remote URL is used as-is, and a local image path relative to and inside the current working directory is auto-uploaded to Lark drive and rendered inline — absolute/out-of-cwd paths are rejected). A Lark doc URL (bare or as a Markdown link) is auto-resolved to an inline doc-mention chip showing its title. Inside a GFM table cell, stack multiple lines with `<br>`; each line may itself be an ordered/unordered list item, image or styled text (e.g. `1. a<br>2. b`, `- x<br>- y`, `![p](url)<br>**bold**`).", Input: []string{common.File, common.Stdin}},
{Name: "description", Desc: "event description as Markdown (@file or - for stdin); the unified description field. Supports bold/italic/underline/strikethrough, links, headings (`#`..`###`), blockquotes (`>`), ordered/unordered lists, horizontal rules (`---`), GFM tables, and images (`![name](url)`; a remote URL is used as-is, and a local image path relative to and inside the current working directory is auto-uploaded to Lark drive and rendered inline — absolute/out-of-cwd paths are rejected). A Lark doc URL (bare or as a Markdown link) is auto-resolved to an inline doc-mention chip showing its title. Inside a GFM table cell, stack multiple lines with `<br>`; each line may itself be an ordered/unordered list item, image or styled text (e.g. `1. a<br>2. b`, `- x<br>- y`, `![p](url)<br>**bold**`). Passing an empty string clears the description.", Input: []string{common.File, common.Stdin}},
{Name: "start", Desc: "new start time (ISO 8601); requires --end"},
{Name: "end", Desc: "new end time (ISO 8601); requires --start"},
{Name: "rrule", Desc: "recurrence rule (rfc5545)"},
@@ -72,7 +71,7 @@ func validateCalendarUpdate(runtime *common.RuntimeContext) error {
return err
}
if !hasCalendarUpdateOperation(runtime) {
return errs.NewValidationError(errs.SubtypeInvalidArgument, "nothing to update: specify at least one of --summary, --description, --description-rich, --start/--end, --rrule, --add-attendee-ids, or --remove-attendee-ids")
return errs.NewValidationError(errs.SubtypeInvalidArgument, "nothing to update: specify at least one of --summary, --description, --start/--end, --rrule, --add-attendee-ids, or --remove-attendee-ids")
}
return nil
}
@@ -114,10 +113,7 @@ func buildCalendarUpdateEventData(runtime *common.RuntimeContext) (map[string]in
body["summary"] = runtime.Str("summary")
hasFields = true
}
if runtime.Cmd.Flags().Changed("description-rich") {
body["description_rich"] = runtime.Str("description-rich")
hasFields = true
} else if runtime.Cmd.Flags().Changed("description") {
if runtime.Cmd.Flags().Changed("description") {
body["description_rich"] = runtime.Str("description")
hasFields = true
}
@@ -362,11 +358,8 @@ func executeCalendarUpdate(ctx context.Context, runtime *common.RuntimeContext)
return errs.NewValidationError(errs.SubtypeInvalidArgument, "specify --event-id").WithParam("--event-id")
}
// Upload any local images referenced in --description-rich and rewrite them
// to drive URLs before the description is sent (the service cannot read
// local files).
if runtime.Cmd.Flags().Changed("description-rich") {
if err := resolveDescriptionRichImages(runtime, calendarID); err != nil {
if runtime.Cmd.Flags().Changed("description") {
if err := resolveDescriptionImages(runtime, calendarID); err != nil {
return err
}
}
@@ -443,19 +436,10 @@ func calendarUpdateResult(eventID string, event map[string]interface{}, addedCou
if summary, _ := event["summary"].(string); summary != "" {
result["summary"] = summary
}
// Surface both description fields on read: description holds plain text,
// description_rich holds the rich (Markdown) version, backfilled from plain
// when the service returned no rich value.
description, _ := event["description"].(string)
if description != "" {
result["description"] = description
}
descriptionRich, _ := event["description_rich"].(string)
if descriptionRich == "" {
descriptionRich = description
}
if descriptionRich != "" {
result["description_rich"] = descriptionRich
if rich, _ := event["description_rich"].(string); rich != "" {
result["description"] = rich
} else if plain, _ := event["description"].(string); plain != "" {
result["description"] = plain
}
if start := formatCalendarEventTime(event["start_time"]); start != "" {
result["start"] = start

View File

@@ -27,8 +27,8 @@ const calendarMediaParentType = "calendar"
var markdownImageRe = regexp.MustCompile(`!\[([^\]]*)\]\(([^)]*)\)`)
func resolveDescriptionRichImages(runtime *common.RuntimeContext, calendarID string) error {
md := runtime.Str("description-rich")
func resolveDescriptionImages(runtime *common.RuntimeContext, calendarID string) error {
md := runtime.Str("description")
if md == "" || !strings.Contains(md, "![") {
return nil
}
@@ -37,8 +37,8 @@ func resolveDescriptionRichImages(runtime *common.RuntimeContext, calendarID str
return err
}
if changed {
if err := runtime.Cmd.Flags().Set("description-rich", rewritten); err != nil {
return errs.NewInternalError(errs.SubtypeUnknown, "failed to update --description-rich after image upload: %v", err).WithCause(err)
if err := runtime.Cmd.Flags().Set("description", rewritten); err != nil {
return errs.NewInternalError(errs.SubtypeUnknown, "failed to update --description after image upload: %v", err).WithCause(err)
}
}
return nil
@@ -85,8 +85,8 @@ func resolveLocalImage(runtime *common.RuntimeContext, calendarID, src, alt stri
safePath, err := validate.SafeInputPath(localPath)
if err != nil {
return "", errs.NewValidationError(errs.SubtypeInvalidArgument,
"--description-rich image %q could not be read: %v", src, err).
WithParam("--description-rich").
"--description image %q could not be read: %v", src, err).
WithParam("--description").
WithHint("reference local images by a path inside the current working directory (e.g. ./images/pic.png; cd there first), or use an already-uploaded Lark image URL").
WithCause(err)
}
@@ -157,7 +157,7 @@ func localImagePath(src string) string {
}
func buildCalendarImagePreviewURL(brand core.LarkBrand, fileToken string, width, height int, size int64) string {
host := "internal-api-drive-stream.larkoffice.com"
host := "internal-api-drive-stream.feishu.cn"
if brand == core.BrandLark {
host = "internal-api-drive-stream.larksuite.com"
}

View File

@@ -68,7 +68,7 @@ func TestBuildCalendarImagePreviewURL(t *testing.T) {
brand core.LarkBrand
hostFrag string
}{
{core.BrandFeishu, "larkoffice"},
{core.BrandFeishu, "feishu.cn"},
{core.BrandLark, "larksuite"},
} {
raw := buildCalendarImagePreviewURL(tc.brand, "boxcnTOKEN123", 416, 306, 142568)
@@ -158,7 +158,7 @@ func TestCreate_UploadsLocalDescriptionImage(t *testing.T) {
"--start", "2025-03-21T00:00:00+08:00",
"--end", "2025-03-21T01:00:00+08:00",
"--calendar-id", "cal_test123",
"--description-rich", "![pic](./pic.png)",
"--description", "![pic](./pic.png)",
"--as", "bot",
}, f, stdout)
if runErr != nil {
@@ -233,7 +233,7 @@ func TestCreate_LocalImageCarriesDimensions(t *testing.T) {
"--start", "2025-03-21T00:00:00+08:00",
"--end", "2025-03-21T01:00:00+08:00",
"--calendar-id", "cal_test123",
"--description-rich", "![pic](./pic.png)",
"--description", "![pic](./pic.png)",
"--as", "bot",
}, f, stdout)
if runErr != nil {
@@ -253,7 +253,7 @@ func TestCreate_LocalImageCarriesDimensions(t *testing.T) {
}
// TestCreate_LocalImageAbsolutePathRejected verifies an out-of-cwd absolute path
// yields a typed --description-rich validation error before any API call.
// yields a typed --description validation error before any API call.
func TestCreate_LocalImageAbsolutePathRejected(t *testing.T) {
f, stdout, _, _ := cmdutil.TestFactory(t, defaultConfig())
@@ -263,7 +263,7 @@ func TestCreate_LocalImageAbsolutePathRejected(t *testing.T) {
"--start", "2025-03-21T00:00:00+08:00",
"--end", "2025-03-21T01:00:00+08:00",
"--calendar-id", "cal_test123",
"--description-rich", "![p](/etc/hosts)",
"--description", "![p](/etc/hosts)",
"--as", "bot",
}, f, stdout)
if runErr == nil {
@@ -273,7 +273,7 @@ func TestCreate_LocalImageAbsolutePathRejected(t *testing.T) {
if !errors.As(runErr, &ve) {
t.Fatalf("expected *errs.ValidationError, got %T: %v", runErr, runErr)
}
if ve.Param != "--description-rich" {
t.Errorf("param = %q, want --description-rich", ve.Param)
if ve.Param != "--description" {
t.Errorf("param = %q, want --description", ve.Param)
}
}

View File

@@ -30,20 +30,23 @@ func resolveStartEnd(runtime *common.RuntimeContext) (string, string) {
return startInput, endInput
}
func backfillDescriptionRich(event map[string]interface{}) {
func collapseDescription(event map[string]interface{}) {
if event == nil {
return
}
if descRich, _ := event["description_rich"].(string); descRich == "" {
if desc, _ := event["description"].(string); desc != "" {
event["description_rich"] = desc
}
rich, _ := event["description_rich"].(string)
plain, _ := event["description"].(string)
delete(event, "description_rich")
switch {
case rich != "":
event["description"] = rich
case plain != "":
event["description"] = plain
default:
delete(event, "description")
}
}
func descriptionRichToSend(runtime *common.RuntimeContext) string {
if v := runtime.Str("description-rich"); v != "" {
return v
}
func descriptionToSend(runtime *common.RuntimeContext) string {
return runtime.Str("description")
}

View File

@@ -16,6 +16,7 @@ import (
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/core"
"github.com/larksuite/cli/internal/httpmock"
"github.com/larksuite/cli/internal/imcontract"
)
func newCallAPITypedRuntime(t *testing.T) (*RuntimeContext, *httpmock.Registry) {
@@ -162,6 +163,19 @@ func TestDoAPIJSONTyped_HTTPErrorWithZeroBodyCodeNotSwallowed(t *testing.T) {
}
}
func TestDoAPIJSONTypedRejectsUnsupportedIMRequestBeforeAPI(t *testing.T) {
rt, _ := newCallAPITypedRuntime(t)
contract, _ := imcontract.Lookup("im messages urgent_app")
rt.contractSession = imcontract.NewSession(contract)
_, err := rt.DoAPIJSONTyped("PATCH", "/open-apis/im/v1/messages/om_x/urgent_app", nil, []any{"not", "an", "object"})
problem, ok := errs.ProblemOf(err)
if !ok || problem.Category != errs.CategoryValidation ||
problem.Subtype != errs.SubtypeInvalidArgument {
t.Fatalf("error = %T %#v", err, problem)
}
}
func TestCallAPITyped_NonJSON5xx(t *testing.T) {
rt, reg := newCallAPITypedRuntime(t)
reg.Register(&httpmock.Stub{

View File

@@ -29,6 +29,7 @@ import (
"github.com/larksuite/cli/internal/credential"
"github.com/larksuite/cli/internal/errclass"
"github.com/larksuite/cli/internal/i18n"
"github.com/larksuite/cli/internal/imcontract"
"github.com/larksuite/cli/internal/output"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@@ -36,20 +37,24 @@ import (
// RuntimeContext provides helpers for shortcut execution.
type RuntimeContext struct {
ctx context.Context // from cmd.Context(), propagated through the call chain
Config *core.CliConfig
Cmd *cobra.Command
Format string
JqExpr string // --jq expression; empty = no filter
outputErrOnce sync.Once // guards first-error capture in Out()/OutFormat()
outputErr error // deferred error from jq filtering; written at most once
botOnly bool // set by framework for bot-only shortcuts
resolvedAs core.Identity // effective identity resolved by framework
Factory *cmdutil.Factory // injected by framework
apiClientFunc func() (*client.APIClient, error) // sync.OnceValues; initialized in newRuntimeContext
botInfoFunc func() (*BotInfo, error) // sync.OnceValues; lazy bot identity from /bot/v3/info
larkSDK *lark.Client // eagerly initialized in mountDeclarative
stdinConsumed bool // set when an Input flag has consumed stdin (`-`); guards against a second flag also using `-` within the same call
ctx context.Context // from cmd.Context(), propagated through the call chain
Config *core.CliConfig
Cmd *cobra.Command
Format string
JqExpr string // --jq expression; empty = no filter
outputErrOnce sync.Once // guards first-error capture in Out()/OutFormat()
outputErr error // deferred error from jq filtering; written at most once
identityWarnOnce sync.Once // emits the defaulted-identity warning at most once
identityDefaulted bool // dual-identity IM write ran without explicit --as
botOnly bool // set by framework for bot-only shortcuts
resolvedAs core.Identity // effective identity resolved by framework
Factory *cmdutil.Factory // injected by framework
apiClientFunc func() (*client.APIClient, error) // sync.OnceValues; initialized in newRuntimeContext
botInfoFunc func() (*BotInfo, error) // sync.OnceValues; lazy bot identity from /bot/v3/info
larkSDK *lark.Client // eagerly initialized in mountDeclarative
stdinConsumed bool // set when an Input flag has consumed stdin (`-`); guards against a second flag also using `-` within the same call
contractSession *imcontract.Session
readSession *imcontract.ReadSession
}
// ── Identity ──
@@ -499,6 +504,20 @@ func (ctx *RuntimeContext) DoAPIStream(callCtx context.Context, req *larkcore.Ap
// auth error from the client boundary is already typed and passes through
// unchanged; a non-zero API code is classified with subtype / code / log_id.
func (ctx *RuntimeContext) DoAPIJSONTyped(method, apiPath string, query larkcore.QueryParams, body any) (map[string]any, error) {
if ctx.contractSession != nil {
requestBody, _ := body.(map[string]any)
if values := query["uuid"]; len(values) > 0 {
cloned := make(map[string]any, len(requestBody)+1)
for key, value := range requestBody {
cloned[key] = value
}
cloned["uuid"] = values[0]
requestBody = cloned
}
if err := ctx.contractSession.ObserveRequest(requestBody); err != nil {
return nil, err
}
}
req := &larkcore.ApiReq{
HttpMethod: method,
ApiPath: apiPath,
@@ -511,7 +530,48 @@ func (ctx *RuntimeContext) DoAPIJSONTyped(method, apiPath string, query larkcore
if err != nil {
return nil, typedOrInternal(err)
}
return ctx.ClassifyAPIResponse(resp)
data, err := ctx.ClassifyAPIResponse(resp)
if ctx.contractSession != nil || ctx.readSession != nil {
logID, _ := logIDFromHeader(resp)["log_id"].(string)
err = imcontract.NormalizeHTTPError(resp.StatusCode, logID, err)
}
if ctx.contractSession != nil && err == nil {
ctx.contractSession.ObserveResponse(data)
}
return data, err
}
// DoWriteAPIJSONTyped marks the narrow point at which a contract-managed
// shortcut starts its target business write, then delegates to the typed JSON
// transport. Preflight and enrichment calls must use DoAPIJSONTyped instead.
func (ctx *RuntimeContext) DoWriteAPIJSONTyped(method, apiPath string, query larkcore.QueryParams, body any) (map[string]any, error) {
ctx.RecordContractFact(imcontract.Fact{Kind: imcontract.FactWriteAttempted})
return ctx.DoAPIJSONTyped(method, apiPath, query, body)
}
// RecordContractFact records one of the small, fixed execution facts that
// cannot be inferred from an API request or response.
func (ctx *RuntimeContext) RecordContractFact(f imcontract.Fact) {
if ctx.contractSession != nil {
ctx.contractSession.RecordFact(f)
}
}
// RecordPagination gives the IM read contract the neutral reason why paging
// stopped. The shortcut does not interpret this status as complete or
// incomplete; that decision belongs to internal/imcontract.
func (ctx *RuntimeContext) RecordPagination(status client.PaginationStatus) {
if ctx.readSession != nil {
ctx.readSession.ObservePagination(status)
}
}
// RecordMaterialization gives the IM read contract the evidence collected
// while resolving search hits into directly consumable message records.
func (ctx *RuntimeContext) RecordMaterialization(status imcontract.MaterializationStatus) {
if ctx.readSession != nil {
ctx.readSession.ObserveMaterialization(status)
}
}
// logIDFromHeader extracts x-tt-logid from response headers and returns it as a detail map.
@@ -673,7 +733,26 @@ func (ctx *RuntimeContext) newEmitter() *output.Emitter {
CommandPath: ctx.Cmd.CommandPath(),
Identity: string(ctx.As()),
ColorEnabled: streams.OutIsTerminal,
NoticeProvider: output.GetNotice,
NoticeProvider: ctx.notice,
})
}
func (ctx *RuntimeContext) notice() map[string]interface{} {
base := output.GetNotice()
if !ctx.identityDefaulted {
return base
}
return imcontract.WithIdentityDefaultedNotice(base, string(ctx.As()))
}
func (ctx *RuntimeContext) warnIdentityDefaulted() {
if !ctx.identityDefaulted {
return
}
ctx.identityWarnOnce.Do(func() {
fmt.Fprintf(ctx.IO().ErrOut, "warning: %s: %s\n",
imcontract.IdentityDefaultedNoticeKey,
imcontract.IdentityDefaultedMessage(string(ctx.As())))
})
}
@@ -700,24 +779,14 @@ func wrapLegacyPrettyRenderer(prettyFn func(w io.Writer)) output.PrettyRenderer
// Out prints a success JSON envelope to stdout.
func (ctx *RuntimeContext) Out(data interface{}, meta *output.Meta) {
ctx.handleEmitterError(ctx.newEmitter().Success(data, output.EmitOptions{
Format: "",
Raw: false,
JQ: ctx.JqExpr,
Meta: meta,
}))
ctx.emitFinalized(data, meta, false, true, "", nil)
}
// OutRaw prints a success JSON envelope to stdout with HTML escaping disabled.
// Use this instead of Out when the data contains XML/HTML content (e.g. document bodies)
// that should be preserved as-is in JSON output.
func (ctx *RuntimeContext) OutRaw(data interface{}, meta *output.Meta) {
ctx.handleEmitterError(ctx.newEmitter().Success(data, output.EmitOptions{
Format: "",
Raw: true,
JQ: ctx.JqExpr,
Meta: meta,
}))
ctx.emitFinalized(data, meta, true, true, "", nil)
}
// OutPartialFailure writes an ok:false multi-status result envelope to stdout
@@ -731,42 +800,146 @@ func (ctx *RuntimeContext) OutRaw(data interface{}, meta *output.Meta) {
// ok:true, and the exit signal is distinct from ErrBare (the
// stdout-carries-the-answer silent-exit signal).
func (ctx *RuntimeContext) OutPartialFailure(data interface{}, meta *output.Meta) error {
ctx.handleEmitterError(ctx.newEmitter().PartialFailure(data, output.EmitOptions{
Format: "",
Raw: false,
JQ: ctx.JqExpr,
Meta: meta,
}))
ctx.emitFinalized(data, meta, false, false, "", nil)
if ctx.outputErr != nil {
return ctx.outputErr
}
return output.PartialFailure(output.ExitAPI)
}
// emitFinalized lets an IM contract determine the business result before the
// command-scoped Emitter performs all safety checks, projection, formatting,
// buffering, and stdout/stderr writes. Non-IM commands pass through unchanged.
func (ctx *RuntimeContext) emitFinalized(
data interface{},
meta *output.Meta,
raw bool,
ok bool,
format string,
pretty output.PrettyRenderer,
) {
hint := ""
var resultExit int
var resultError interface{}
var resultCause error
var contractResult imcontract.Result
hasContractResult := false
if ctx.contractSession != nil {
result, err := ctx.contractSession.FinalizeSuccess(data)
if err != nil {
ctx.outputErrOnce.Do(func() { ctx.outputErr = err })
return
}
contractResult = result
hasContractResult = true
data = result.Data
ok = result.OK
hint = result.Hint
resultExit = result.ExitCode
}
if ctx.readSession != nil {
result, err := ctx.readSession.Finalize(data)
if err != nil {
ctx.outputErrOnce.Do(func() { ctx.outputErr = err })
return
}
data = result.Data
ok = result.OK
meta = mergeIMReadMeta(meta, result.Meta)
hint = result.Hint
resultExit = result.ExitCode
if result.Error != nil {
resultError = result.Error
}
resultCause = result.Cause
}
ctx.warnIdentityDefaulted()
// Legacy OutFormat falls back to the JSON envelope when a command does not
// provide a pretty renderer. Preserve that behavior without re-finalizing
// the contract or introducing another output path.
if format == "pretty" && pretty == nil {
format = ""
}
emitOpts := output.EmitOptions{
Format: format,
Raw: raw,
JQ: ctx.JqExpr,
Meta: meta,
Error: resultError,
Hint: hint,
Pretty: pretty,
// Structured JSON carries the hint in-band. Projected reads and naked
// formats need the recovery guidance on stderr so it is not discarded.
HintToStderr: hint != "" &&
((ctx.readSession != nil && ctx.JqExpr != "") ||
(ctx.JqExpr == "" && format != "" && format != "json")),
}
emitter := ctx.newEmitter()
var emitErr error
if !ok && (ctx.JqExpr != "" || format == "" || format == "json") {
emitErr = emitter.PartialFailure(data, emitOpts)
} else {
emitErr = emitter.Success(data, emitOpts)
}
if emitErr != nil {
if hasContractResult {
if errs.IsContentSafety(emitErr) {
ctx.writeIMContentSafetyFallback(contractResult)
return
}
if ctx.JqExpr != "" {
fmt.Fprintln(ctx.IO().ErrOut, "error: jq projection failed after the IM write completed; inspect --jq")
ctx.writeIMJQFallback(contractResult)
return
}
}
ctx.handleEmitterError(emitErr)
return
}
if resultExit != 0 {
ctx.outputErrOnce.Do(func() {
if resultCause != nil &&
(ctx.JqExpr != "" || (format != "" && format != "json")) {
ctx.outputErr = resultCause
return
}
ctx.outputErr = output.PartialFailure(resultExit)
})
}
}
// OutFormat prints output based on --format flag.
// "json" (default) outputs JSON envelope; "pretty" calls prettyFn; others delegate to FormatValue.
// When JqExpr is set, envelope filtering takes precedence over format.
// The Emitter handles content safety scanning for every format.
func (ctx *RuntimeContext) OutFormat(data interface{}, meta *output.Meta, prettyFn func(w io.Writer)) {
ctx.handleEmitterError(ctx.newEmitter().Success(data, output.EmitOptions{
Format: ctx.Format,
Raw: false,
JQ: ctx.JqExpr,
Meta: meta,
Pretty: wrapLegacyPrettyRenderer(prettyFn),
}))
ctx.emitFinalized(data, meta, false, true, ctx.Format, wrapLegacyPrettyRenderer(prettyFn))
}
// OutFormatRaw is like OutFormat but with HTML escaping disabled in JSON output.
// Use this when the data contains XML/HTML content that should be preserved as-is.
func (ctx *RuntimeContext) OutFormatRaw(data interface{}, meta *output.Meta, prettyFn func(w io.Writer)) {
ctx.handleEmitterError(ctx.newEmitter().Success(data, output.EmitOptions{
Format: ctx.Format,
Raw: true,
JQ: ctx.JqExpr,
Meta: meta,
Pretty: wrapLegacyPrettyRenderer(prettyFn),
}))
ctx.emitFinalized(data, meta, true, true, ctx.Format, wrapLegacyPrettyRenderer(prettyFn))
}
func (ctx *RuntimeContext) writeIMJQFallback(result imcontract.Result) {
env, signal := imcontract.BuildJQOutputFallback(result)
if err := ctx.newEmitter().RedactedFallback(env); err != nil {
ctx.handleEmitterError(err)
return
}
ctx.outputErrOnce.Do(func() { ctx.outputErr = signal })
}
func (ctx *RuntimeContext) writeIMContentSafetyFallback(result imcontract.Result) {
env, signal := imcontract.BuildContentSafetyOutputFallback(result)
if err := ctx.newEmitter().RedactedFallback(env); err != nil {
ctx.handleEmitterError(err)
return
}
ctx.outputErrOnce.Do(func() { ctx.outputErr = signal })
}
// ── Scope pre-check ──
@@ -863,6 +1036,10 @@ func (s Shortcut) mountDeclarative(ctx context.Context, parent *cobra.Command, f
}
cmdmeta.SetSource(cmd, cmdmeta.SourceShortcut, false)
cmdmeta.SetAffordanceRef(cmd, shortcut.Service, shortcut.Command)
contractKey := imcontract.ContractKey(shortcut.Service + " " + shortcut.Command)
if _, ok := imcontract.Lookup(contractKey); ok {
imcontract.AnnotateHelpContract(cmd, contractKey)
}
cmdutil.SetSupportedIdentities(cmd, shortcut.AuthTypes)
registerShortcutFlagsWithContext(ctx, cmd, f, &shortcut)
cmdutil.SetTips(cmd, shortcut.Tips)
@@ -946,6 +1123,12 @@ func runShortcut(cmd *cobra.Command, f *cmdutil.Factory, s *Shortcut, botOnly bo
}
if err := s.Execute(rctx.ctx, rctx); err != nil {
if rctx.contractSession != nil {
return rctx.contractSession.FinalizeError(err)
}
if rctx.readSession != nil {
return rctx.readSession.FinalizeError(err)
}
return err
}
return rctx.outputErr
@@ -989,6 +1172,21 @@ func newRuntimeContext(cmd *cobra.Command, f *cmdutil.Factory, s *Shortcut, conf
ctx := cmd.Context()
ctx = cmdutil.ContextWithShortcut(ctx, s.Service+":"+s.Command, uuid.New().String())
rctx := &RuntimeContext{ctx: ctx, Config: config, Cmd: cmd, botOnly: botOnly, resolvedAs: as, Factory: f}
if contract, ok := imcontract.Lookup(imcontract.ContractKey(s.Service + " " + s.Command)); ok {
switch {
case contract.Strategy.Kind.IsWrite():
rctx.contractSession = imcontract.NewSession(contract)
rctx.identityDefaulted = shortcutIdentityWasDefaulted(cmd, f, s)
case contract.Strategy.Kind.IsRead():
readSession, readErr := imcontract.NewReadSession(contract, imcontract.ReadOptions{
FullRead: imContractFullRead(cmd, contract.Key),
})
if readErr != nil {
return nil, readErr
}
rctx.readSession = readSession
}
}
rctx.apiClientFunc = sync.OnceValues(func() (*client.APIClient, error) {
return f.NewAPIClientWithConfig(config)
})
@@ -1006,6 +1204,55 @@ func newRuntimeContext(cmd *cobra.Command, f *cmdutil.Factory, s *Shortcut, conf
return rctx, nil
}
func shortcutIdentityWasDefaulted(cmd *cobra.Command, f *cmdutil.Factory, s *Shortcut) bool {
if cmd == nil || f == nil || s == nil || cmd.Flags().Changed("as") ||
!f.IdentityAutoDetected || f.ResolveStrictMode(cmd.Context()).IsActive() {
return false
}
return slices.Contains(s.AuthTypes, string(core.AsUser)) &&
slices.Contains(s.AuthTypes, string(core.AsBot))
}
func shortcutBoolFlag(cmd *cobra.Command, name string) bool {
if cmd == nil || cmd.Flags().Lookup(name) == nil {
return false
}
value, _ := cmd.Flags().GetBool(name)
return value
}
func imContractFullRead(cmd *cobra.Command, key imcontract.ContractKey) bool {
if shortcutBoolFlag(cmd, "page-all") {
return true
}
if key != "im +messages-search" || cmd == nil {
return false
}
flag := cmd.Flags().Lookup("page-limit")
if flag == nil || !flag.Changed {
return false
}
limit, err := cmd.Flags().GetInt("page-limit")
return err == nil && limit == 0
}
func mergeIMReadMeta(base, contract *output.Meta) *output.Meta {
if base == nil && contract == nil {
return nil
}
merged := output.Meta{}
if base != nil {
merged = *base
}
if contract != nil {
merged.Complete = contract.Complete
merged.PagesFetched = contract.PagesFetched
merged.StopReason = contract.StopReason
merged.NextPageToken = contract.NextPageToken
}
return &merged
}
// stripUTF8BOM removes a leading UTF-8 byte-order mark from content read from a
// file or stdin. A BOM that survives into a CSV cell corrupts the first value
// (e.g. "\ufeffNorth", which then makes a MAXIFS/lookup miss it), and a BOM at the
@@ -1131,13 +1378,15 @@ func handleShortcutDryRun(f *cmdutil.Factory, rctx *RuntimeContext, s *Shortcut)
// Same data.context contract as the service/api dry-run paths.
dryResult.Context(rctx.Config.AppID, rctx.UserOpenId())
}
rctx.warnIdentityDefaulted()
return cmdutil.WriteDryRun(dryResult, cmdutil.DryRunOutputOptions{
Format: rctx.Format,
JqExpr: rctx.JqExpr,
CommandPath: rctx.Cmd.CommandPath(),
Identity: rctx.As(),
Out: f.IOStreams.Out,
ErrOut: f.IOStreams.ErrOut,
Format: rctx.Format,
JqExpr: rctx.JqExpr,
CommandPath: rctx.Cmd.CommandPath(),
Identity: rctx.As(),
Out: f.IOStreams.Out,
ErrOut: f.IOStreams.ErrOut,
NoticeProvider: rctx.notice,
})
}

View File

@@ -8,6 +8,8 @@ import (
"context"
"encoding/json"
"errors"
"io"
"strings"
"testing"
"github.com/spf13/cobra"
@@ -16,6 +18,7 @@ import (
extcs "github.com/larksuite/cli/extension/contentsafety"
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/core"
"github.com/larksuite/cli/internal/imcontract"
"github.com/larksuite/cli/internal/output"
)
@@ -94,6 +97,59 @@ func TestOut_ContentSafetyBlock(t *testing.T) {
}
}
func TestIMContractWriteContentSafetyBlockKeepsAllowlistedCompletion(t *testing.T) {
const secret = "SECRET_MARKER"
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "block")
alert := &extcs.Alert{Provider: "test", MatchedRules: []string{secret}}
extcs.Register(&csTestProvider{alert: alert})
defer extcs.Register(nil)
rctx, stdout, stderr := newCSTestContext(t)
rctx.Format = "pretty"
contract, _ := imcontract.Lookup("im chat.moderation update")
rctx.contractSession = imcontract.NewSession(contract)
prettyCalled := false
rctx.OutFormat(map[string]any{"subject": secret}, nil, func(io.Writer) {
prettyCalled = true
})
if prettyCalled {
t.Fatal("blocked pretty presentation ran after the write completed")
}
if output.ExitCodeOf(rctx.outputErr) != output.ExitContentSafety {
t.Fatalf("output error = %T %v", rctx.outputErr, rctx.outputErr)
}
if stderr.Len() != 0 {
t.Fatalf("stderr = %q, want empty", stderr.String())
}
if bytes.Contains(stdout.Bytes(), []byte(secret)) || strings.Contains(rctx.outputErr.Error(), secret) {
t.Fatalf("blocked payload or scanner detail leaked: stdout=%q err=%v", stdout.String(), rctx.outputErr)
}
var env map[string]any
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
t.Fatalf("fallback is not JSON: %v\n%s", err, stdout.String())
}
if len(env) != 3 || env["ok"] != false {
t.Fatalf("fallback = %#v", env)
}
data, _ := env["data"].(map[string]any)
completion, _ := data["completion"].(map[string]any)
if len(data) != 1 || completion["status"] != "accepted_unverified" ||
completion["final_state_verified"] != false || completion["retry_scope"] != "none" {
t.Fatalf("completion = %#v", completion)
}
problem, _ := env["error"].(map[string]any)
if problem["type"] != "policy" || problem["subtype"] != "content_safety" ||
problem["message"] != "Output blocked after the IM write completed" {
t.Fatalf("error = %#v", problem)
}
if _, exists := env["presentation"]; exists {
t.Fatalf("fallback introduced presentation: %#v", env)
}
}
func TestOut_ContentSafetyOff(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "off")

Some files were not shown because too many files have changed in this diff Show More