From fb1d54bdeaa0ef79f8bb4e8e6ef73bc59e1ffef7 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 29 Nov 2026 22:46:17 +0530 Subject: [PATCH 1/4] feat(kin): owner edits the standing rules on web and phone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings → Standing rules, beside Memory on web (and in the command palette) and under Agents in the phone's Settings, for the owner alone. The owner reads the rules, edits them as plain text and saves over the revision opened. A save that finds them changed keeps the draft and shows the current rules above it; only the owner's next Save replaces them. Emptying them asks first, text over 256 KiB says how far over, and leaving with a draft asks before discarding it. People-change: The owner can read and change the standing rules every agent follows from Settings on web and phone, and agents follow a saved change from their next turn. Co-Authored-By: Claude Opus 5.5 --- .../src/features/chats/KinRulesScreen.tsx | 291 ++++++++++++++ .../mobile/src/features/chats/KinSettings.tsx | 9 +- apps/mobile/src/features/chats/routes.tsx | 7 + .../src/kin/featureMap/feature-map.yaml | 18 +- .../src/kin/featureMap/featureMap.test.ts | 2 +- apps/web/src/kin/KinSettingsView.tsx | 8 + .../settings/KinRulesPage.browser.test.tsx | 132 ++++++ apps/web/src/kin/settings/KinRulesPage.tsx | 253 ++++++++++++ apps/web/src/kin/useKinPalette.tsx | 30 ++ docs/user/kin.md | 17 +- packages/client-runtime/src/kin/kinClient.ts | 21 + packages/client-runtime/src/kin/kinGitHub.ts | 11 +- .../src/kin/kinNavigation.test.ts | 17 + .../client-runtime/src/kin/kinNavigation.ts | 31 +- .../client-runtime/src/kin/kinRules.test.ts | 249 ++++++++++++ packages/client-runtime/src/kin/kinRules.ts | 380 ++++++++++++++++++ 16 files changed, 1462 insertions(+), 14 deletions(-) create mode 100644 apps/mobile/src/features/chats/KinRulesScreen.tsx create mode 100644 apps/web/src/kin/settings/KinRulesPage.browser.test.tsx create mode 100644 apps/web/src/kin/settings/KinRulesPage.tsx create mode 100644 packages/client-runtime/src/kin/kinRules.test.ts create mode 100644 packages/client-runtime/src/kin/kinRules.ts diff --git a/apps/mobile/src/features/chats/KinRulesScreen.tsx b/apps/mobile/src/features/chats/KinRulesScreen.tsx new file mode 100644 index 000000000000..90fc4f68777d --- /dev/null +++ b/apps/mobile/src/features/chats/KinRulesScreen.tsx @@ -0,0 +1,291 @@ +import { useAtomValue } from "@effect/atom-react"; +import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; +import { KIN_RETRY } from "@t3tools/client-runtime/kin/kinAnswers"; +import { KIN_THIS_PHONE_COPY } from "@t3tools/client-runtime/kin/kinDevice"; +import { + KIN_RULES_COPY, + KIN_RULES_NO_EDITS, + createKinRulesEditor, + kinRulesProblem, + kinRulesView, + type KinRulesView, +} from "@t3tools/client-runtime/kin/kinRules"; +import { KIN_FORM_DISCARD } from "@t3tools/client-runtime/kin/kinWorkForms"; +import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; +import { EnvironmentId, KinOrgId, type KinMemoryRules } from "@t3tools/contracts"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import { AsyncResult, Atom } from "effect/reactivity"; +import * as Clipboard from "expo-clipboard"; +import { useState } from "react"; +import { View } from "react-native"; + +import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; +import { AppText as Text } from "../../components/ui/AppText"; +import { Button } from "../../components/ui/Button"; +import * as Dialog from "../../components/ui/Dialog"; +import { EmptyState } from "../../components/ui/EmptyState"; +import { ListSection } from "../../components/ui/ListSection"; +import { Notice } from "../../components/ui/Notice"; +import { appAtomRegistry } from "../../state/atom-registry"; +import { useAtomCommand } from "../../state/use-atom-command"; +import { SettingsRow } from "../settings/components/SettingsRow"; +import { + KinGateView, + KinWorkFrame, + KinWorkTextField, + useKinAskBeforeDiscard, +} from "./components/KinWorkPieces"; +import { kinClient } from "./state/kinClient"; +import { useKinGate } from "./state/useKinHost"; + +export type KinRulesParams = { readonly environmentId: string; readonly orgId: string }; + +interface KinRulesTarget { + readonly environmentId: EnvironmentId; + readonly orgId: KinOrgId; +} + +const decodeEnvironmentId = Schema.decodeUnknownOption(EnvironmentId); +const decodeOrgId = Schema.decodeUnknownOption(KinOrgId); + +type RulesResult = AsyncResult.AsyncResult; +// Read only by the owner's session; anyone else asks the host nothing. +const noRulesAtom: Atom.Atom = Atom.make(AsyncResult.initial()); + +/** Settings' way in, for the owner alone: absent for everyone else. */ +export function KinRulesSettingsRow(props: KinRulesTarget) { + const navigation = useNavigation(); + const allowed = useAtomValue(kinClient.memorySaveRules.permissionAtom(props.environmentId)); + if (!allowed) return null; + return ( + navigation.navigate("KinRules", props)} + /> + ); +} + +/** The standing rules every agent follows: the owner's alone, to read and to change. */ +export function KinRulesScreen({ route }: StaticScreenProps) { + const navigation = useNavigation(); + const environmentId = Option.getOrNull(decodeEnvironmentId(route.params.environmentId)); + const orgId = Option.getOrNull(decodeOrgId(route.params.orgId)); + if (environmentId === null || orgId === null) + return ( + navigation.goBack()}> + + + ); + return ; +} + +function KinRulesBody({ environmentId, orgId }: KinRulesTarget) { + const navigation = useNavigation(); + const gate = useKinGate( + kinClient.memorySaveRules.permissionStateAtom(environmentId), + environmentId, + ); + const rulesAtom = kinClient.memoryRules({ environmentId, input: { orgId } }); + const result = useAtomValue(gate === null ? rulesAtom : noRulesAtom); + const saveRules = useAtomCommand(kinClient.memorySaveRules, { reportFailure: false }); + const [edits, setEdits] = useState(KIN_RULES_NO_EDITS); + const [editor] = useState(() => + createKinRulesEditor({ + orgId, + device: "phone", + save: (input) => saveRules({ environmentId, input }), + update: setEdits, + refresh: () => appAtomRegistry.refresh(rulesAtom), + }), + ); + // What was last read stays up while it is read again, and the draft stays over it. + const read = Option.getOrNull(AsyncResult.value(result)); + const view = read === null ? null : kinRulesView(read, edits); + const guarded = view !== null && view.dirty && !view.saving; + useKinAskBeforeDiscard(guarded); + + const save = async (confirmed = false) => { + if (read === null) return; + if ((await editor.save(read, edits, confirmed)) !== "ask") return; + Dialog.alert(KIN_RULES_COPY.withdraw.title, KIN_RULES_COPY.withdraw.detail, [ + { text: KIN_FORM_DISCARD.keep, style: "cancel" }, + { + text: KIN_RULES_COPY.withdraw.action, + style: "destructive", + onPress: () => void save(true), + }, + ]); + }; + + return ( + navigation.goBack()} + {...(view === null + ? {} + : { + confirm: { + label: view.saving ? KIN_RULES_COPY.saving : KIN_RULES_COPY.save, + onPress: () => void save(), + loading: view.saving, + disabled: !view.canSave, + }, + })} + > + {gate !== null ? ( + + ) : read === null || view === null ? ( + AsyncResult.isFailure(result) ? ( + appAtomRegistry.refresh(rulesAtom)} /> + ) : ( + + + + ) + ) : ( + <> + + {view.empty ? KIN_RULES_COPY.empty : KIN_RULES_COPY.purpose} + + {view.problem === null ? null : ( + navigation.navigate("KinThisPhone"), + }, + } + : {})} + /> + )} + {view.changed === null ? null : ( + editor.takeCurrent(read)} + /> + )} + + editor.type(view.held, text)} + /> + + {view.facts.length === 0 ? null : ( + + {view.facts.join("\n")} + + )} + + )} + + ); +} + +function KinRulesLoadFailed(props: { + readonly result: AsyncResult.Failure; + readonly onRetry: () => void; +}) { + const navigation = useNavigation(); + const problem = kinRulesProblem(squashAtomCommandFailure(props.result), "", "phone"); + return ( + } + : problem.kind === "pair_again" + ? { + action: ( +