mirror of
https://github.com/larksuite/cli.git
synced 2026-07-08 01:51:27 +08:00
AI agents running inside OpenClaw / Hermes were routinely creating a parallel
app via `config init --new` instead of binding to the agent's existing app,
because every "not configured" hint and several deny errors hard-coded
`config init` regardless of workspace. Once bound, the same agents could
silently grant themselves user identity (impersonation) without the user
ever seeing a risk message in chat.
Changes:
- Introduce `core.NotConfiguredError` / `NoActiveProfileError` /
`reconfigureHint` helpers that branch on `CurrentWorkspace()`. In agent
workspaces they point at `lark-cli config bind --help` (a help page, not
a ready-to-run command) so AI must read the binding workflow and confirm
identity preset with the user before acting. In local terminals they
preserve the previous `config init --new` guidance.
- Migrate every `config init` hint that should be workspace-aware:
RequireConfigForProfile, default credential provider, credential provider
fallback, secret-resolve mismatch, config show, strict-mode entry-point
errors, default-as, profile use/rename/remove, auth list, doctor's
config_file check (which now also wraps the OS-level "no such file"
noise into the user-shaped "not configured" message).
- Refuse `config init` when run inside an OpenClaw / Hermes workspace by
default; add `--force-init` for the rare case the user genuinely wants
a parallel app. Without this guard, hint fixes were undone the moment
AI ignored them.
- Rewrite the strict-mode deny errors in cmd/auth/login.go, cmd/prune.go,
and internal/cmdutil/factory.go. The previous "AI agents are strictly
prohibited from modifying this setting" terminated AI reasoning while
providing no real gate. New errors point at `config strict-mode --help`
with the legitimate confirmation flow and explicitly note that switching
does NOT require re-bind. Integration test envelopes updated.
- Tighten `config bind --help` and `config strict-mode --help` to encode
the user-confirmation discipline directly: identity preset semantics
(bot-only vs user-default), "DO NOT switch without explicit user
confirmation", and a cross-reference clarifying that `config bind` is
for changing the underlying app while `config strict-mode` is the
policy-only switch (resolves an ambiguity an audit run found).
- Surface user-identity (impersonation) risk at every config write that
newly grants it, by reusing the canonical IdentityEscalationMessage
string from bind_messages.go:
- `noticeUserDefaultRisk` fires on flag-mode bind landing on
user-default, including the first-time case `warnIdentityEscalation`
misses (it requires a previous bot lock).
- `setStrictMode` warns when transitioning bot → user or bot → off
(newly permits user identity); stays quiet on narrowing changes
and on off → user (off already permitted user).
- Add tests: notconfigured_test.go (workspace branches),
init_guard_test.go (refuse + --force-init bypass), bind_warning_test.go
(user-default warning fires; bot-only does not), strict_mode_warning_test.go
(5 transitions covering both warn and no-warn paths).
Two follow-ups intentionally deferred: the keychain master-key hint at
internal/keychain/keychain.go:42 still suggests `config init` because the
keychain package can't import core (would be circular); fixing requires
either parameterizing the hint via callback or extracting workspace into
its own package. The lark-shared skill doc still tells AI to run
`config init` for first-time setup; updating the skill is in scope for
a follow-up PR.
Change-Id: I02273e044d9e061d211ceaa4f3ed5a3fb28325b3
121 lines
4.5 KiB
Go
121 lines
4.5 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package core
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// LoadOrNotConfigured wraps LoadMultiAppConfig with the standard "not yet
|
|
// configured vs. couldn't read" disambiguation that every config-required
|
|
// command should use:
|
|
//
|
|
// - file missing → workspace-aware NotConfiguredError (init / bind hint)
|
|
// - parse error / permission error → real load failure with the original
|
|
// cause preserved, so the user can actually fix the broken file
|
|
//
|
|
// Without this, every call site that did `if err != nil { return
|
|
// NotConfiguredError() }` silently coerced corrupt-config into "run init",
|
|
// which sent users in circles when their config.json was just malformed.
|
|
func LoadOrNotConfigured() (*MultiAppConfig, error) {
|
|
multi, err := LoadMultiAppConfig()
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil, NotConfiguredError()
|
|
}
|
|
// Surface the real cause (parse error, permission denied, etc.)
|
|
// so the user can fix the broken file. Wrapping as ConfigError
|
|
// keeps it on the standard structured-envelope path at the root
|
|
// command's error sink.
|
|
return nil, &ConfigError{
|
|
Code: 2,
|
|
Type: "config",
|
|
Message: fmt.Sprintf("failed to load config: %v", err),
|
|
}
|
|
}
|
|
if multi == nil || len(multi.Apps) == 0 {
|
|
return nil, NotConfiguredError()
|
|
}
|
|
return multi, nil
|
|
}
|
|
|
|
const (
|
|
// localInitHint is the canonical "you're in a regular terminal, run
|
|
// init" guidance — shared by NotConfiguredError and NoActiveProfileError
|
|
// so the same session can't show two different recommended commands.
|
|
localInitHint = "run `lark-cli config init --new` in the background. It blocks and outputs a verification URL — retrieve the URL and open it in a browser to complete setup."
|
|
|
|
// agentBindHint is the canonical "you're in an Agent workspace, see
|
|
// the binding workflow" guidance. Always points at --help (never a
|
|
// ready-to-run bind command) so the AI reads the confirmation
|
|
// discipline (identity preset, user opt-in) before acting.
|
|
agentBindHint = "read `lark-cli config bind --help`, then ask the user to confirm intent and identity preset (bot-only or user-default); only after both are confirmed, run `lark-cli config bind`"
|
|
)
|
|
|
|
// NotConfiguredError returns the canonical "not configured" error, with a
|
|
// hint that depends on the active workspace:
|
|
//
|
|
// - WorkspaceLocal → suggest `config init --new` (creates a new app).
|
|
// - WorkspaceOpenClaw / WorkspaceHermes → point at `config bind --help`
|
|
// rather than a ready-to-run command, because binding is policy-laden:
|
|
// the user must pick an identity preset (bot-only vs user-default),
|
|
// and re-binding may overwrite an existing one. The help text walks
|
|
// the AI through the confirmation flow.
|
|
//
|
|
// All "config not loaded yet" call sites should use this helper rather than
|
|
// hand-rolling a hint, so AI agents always get a workspace-correct next step.
|
|
func NotConfiguredError() error {
|
|
ws := CurrentWorkspace()
|
|
if ws.IsLocal() {
|
|
return &ConfigError{
|
|
Code: 2,
|
|
Type: "config",
|
|
Message: "not configured",
|
|
Hint: localInitHint,
|
|
}
|
|
}
|
|
return &ConfigError{
|
|
Code: 2,
|
|
Type: ws.Display(),
|
|
Message: fmt.Sprintf("%s context detected but lark-cli is not bound to it", ws.Display()),
|
|
Hint: agentBindHint,
|
|
}
|
|
}
|
|
|
|
// reconfigureHint returns the workspace-aware "fix it from scratch" hint
|
|
// used by error paths that aren't full ConfigErrors (e.g. plain fmt.Errorf
|
|
// strings from keychain / secret validation). Local → `config init`;
|
|
// Agent → `config bind --help` so the AI reads the binding workflow and
|
|
// confirms identity preset with the user before running the actual command.
|
|
func reconfigureHint() string {
|
|
if CurrentWorkspace().IsLocal() {
|
|
return "please run `lark-cli config init` to reconfigure"
|
|
}
|
|
return agentBindHint
|
|
}
|
|
|
|
// NoActiveProfileError mirrors NotConfiguredError for the related
|
|
// "config exists but the requested profile cannot be resolved" case. In agent
|
|
// workspaces a missing profile typically means the binding was wiped while
|
|
// the workspace marker remained — re-binding is the correct fix, not init.
|
|
func NoActiveProfileError() error {
|
|
ws := CurrentWorkspace()
|
|
if ws.IsLocal() {
|
|
return &ConfigError{
|
|
Code: 2,
|
|
Type: "config",
|
|
Message: "no active profile",
|
|
Hint: localInitHint,
|
|
}
|
|
}
|
|
return &ConfigError{
|
|
Code: 2,
|
|
Type: ws.Display(),
|
|
Message: fmt.Sprintf("no active profile in %s workspace", ws.Display()),
|
|
Hint: agentBindHint,
|
|
}
|
|
}
|