Urbit
  • Introduction
  • Development

    • Getting Started
    • Environment Setup
    • Grants Program
    • Project Repositories
    • Precepts
    • System Overview

      • Arvo
      • Hoon
      • Nock
      • Vere
      • Azimuth
      • Cryptography
      • Arvo

        • Overview
        • Ames

          • Overview
          • Cryptography
          • API Reference
          • Scry Reference
          • Data Types
          • Behn

            • Overview
            • API Reference
            • Scry Reference
            • Examples
            • Clay

              • Overview
              • Architecture
              • Using Clay
              • Data Types
              • Scry Reference
              • API Reference
              • Examples
              • Marks

                • Overview
                • Writing Marks
                • Using Marks
                • Examples
              • Dill

                • Overview
                • API Reference
                • Scry Reference
                • Data Types
                • Eyre

                  • Overview
                  • External API Reference
                  • Internal API Reference
                  • Scry Reference
                  • Data Types
                  • Guide
                  • Ford

                    • Overview
                    • Gall

                      • Overview
                      • API Reference
                      • Data Types
                      • Iris

                        • Overview
                        • API Reference
                        • Data Types
                        • Example
                        • Jael

                          • Overview
                          • API Reference
                          • Scry Reference
                          • Data Types
                          • Examples
                          • Concepts

                            • Scries
                            • Subscriptions
                            • Tutorials

                              • Move Trace
                              • Reference

                                • Cryptography
                                • Filesystem Hierarchy
                              • Userspace

                                • Overview
                                • HTTP API Guide
                                • Gall Guide

                                  • Introduction
                                  • 1. Arvo
                                  • 2. The Agent Core
                                  • 3. Imports and Aliases
                                  • 4. Lifecycle
                                  • 5. Cards
                                  • 6. Pokes
                                  • 7. Structures and Marks
                                  • 8. Subscriptions
                                  • 9. Vanes
                                  • 10. Scries
                                  • 11. Failure
                                  • 12. Next Steps
                                  • Appendix: Types
                                  • Full-Stack Walkthrough

                                    • 1. Introduction
                                    • 2. Types
                                    • 3. Agent
                                    • 4. JSON
                                    • 5. Marks
                                    • 6. Eyre
                                    • 7. React app setup
                                    • 8. React app logic
                                    • 9. Desk and glob
                                    • 10. Summary
                                    • Graph Store

                                      • Graph Store Overview
                                      • Data Structure Overview
                                      • Validator Walkthrough
                                      • Sample Application: Library
                                      • Graph Store - Reference
                                      • Advanced Info
                                      • Threads

                                        • Overview
                                        • HTTP API
                                        • Reference
                                        • Basics

                                          • Fundamentals
                                          • Bind
                                          • Input
                                          • Output
                                          • Summary
                                          • Gall

                                            • Start Thread
                                            • Take Result
                                            • Take Facts
                                            • Stop Thread
                                            • Poke Thread
                                            • Examples

                                              • Fetch JSON
                                              • Child Thread
                                              • Main-loop
                                              • Poke Agent
                                              • Scry
                                              • Take Fact
                                            • Distribution

                                              • Overview
                                              • Guide
                                              • Docket File
                                              • Glob
                                              • Dojo Tools
                                            • Hoon

                                              • Overview
                                              • Hoon School

                                                • 1.1 Introduction
                                                • 1.1.1 Walkthrough: List of Numbers
                                                • 1.2 Nouns
                                                • 1.3 Hoon Syntax
                                                • 1.3.1 Walkthrough: Conditionals
                                                • 1.4 Gates (Hoon Functions)
                                                • 1.4.1 Walkthrough: Recursion
                                                • 1.5 Lists
                                                • 1.5.1 Walkthrough: Fibonacci Sequence
                                                • 1.6 The Subject and Its Legs
                                                • 1.6.1 Walkthrough: Ackermann Function
                                                • 1.7 Arms and Cores
                                                • 1.7.1 Walkthrough: Caesar Cipher
                                                • 1.8 Doors
                                                • 1.8.1 Bank Account
                                                • 1.9 Generators
                                                • 2.1 Atoms, Auras, and Simple Cell Types
                                                • 2.2 Type Checking and Type Inference
                                                • 2.3 Structures and Complex Types
                                                • 2.3.1 Walkthrough: Libraries
                                                • 2.3.2 Molds
                                                • 2.4 Standard Library: Trees, Sets, and Maps
                                                • 2.5 Type Polymorphism
                                                • 2.5.1 Walkthrough: Iron Polymorphism and Wet Polymorphism
                                                • 2.5.2 Walkthrough: Lead Polymorphism
                                                • 2.6 Behn
                                                • 2.7 Gall
                                                • 2.7.1 Gall Walkthrough: Egg Timer
                                                • Guides

                                                  • CLI apps
                                                  • Parsing
                                                  • JSON
                                                  • Strings
                                                  • Sail (HTML)
                                                  • Writing Aqua Tests
                                                  • Reference

                                                    • Cheat Sheet
                                                    • Irregular forms
                                                    • Hoon Errors
                                                    • Hoon Style Guide
                                                    • Basic Types
                                                    • Advanced Types
                                                    • Auras
                                                    • Runes

                                                      • Atoms and strings
                                                      • Nock . ('dot')
                                                      • Wild ! ('zap')
                                                      • Change Subject = ('tis')
                                                      • Conditionals ? ('wut')
                                                      • Cores | ('bar')
                                                      • Arms + ('lus')
                                                      • Cells : ('col')
                                                      • Calls % ('cen')
                                                      • Casts ^ ('ket')
                                                      • Structures $ ('buc')
                                                      • Make ; ('mic')
                                                      • Hints ~ ('sig')
                                                      • Imports / ('fas')
                                                      • Terminators -- and ==
                                                      • Limbs and wings

                                                        • Limbs
                                                        • Wings
                                                        • Standard library

                                                          • Table of Contents
                                                          • 1a: Basic Arithmetic
                                                          • 1b: Tree Addressing
                                                          • 1c: Molds and Mold-Builders
                                                          • 2a: Unit Logic
                                                          • 2b: List Logic
                                                          • 2c: Bit Arithmetic
                                                          • 2d: Bit Logic
                                                          • 2e: Insecure Hashing
                                                          • 2f: Noun Ordering
                                                          • 2g: Unsigned Powers
                                                          • 2h: Set Logic
                                                          • 2i: Map Logic
                                                          • 2j: Jar and Jug Logic
                                                          • 2k: Queue Logic
                                                          • 2l: Container from Container
                                                          • 2m: Container from Noun
                                                          • 2n: Functional Hacks
                                                          • 2o: Normalizing Containers
                                                          • 2p: Serialization
                                                          • 2q: Molds and Mold-Builders
                                                          • 3a: Modular and Signed Ints
                                                          • 3b: Floating Point
                                                          • 3c: Urbit Time
                                                          • 3d: SHA Hash Family
                                                          • 3e: AES encryption (Removed)
                                                          • 3f: Scrambling
                                                          • 3g: Molds and Mold-Builders
                                                          • 4a: Exotic Bases
                                                          • 4b: Text Processing
                                                          • 4c: Tank Printer
                                                          • 4d: Parsing (Tracing)
                                                          • 4e: Parsing (Combinators)
                                                          • 4f: Parsing (Rule-Builders)
                                                          • 4g: Parsing (Outside Caller)
                                                          • 4h: Parsing (ASCII Glyphs)
                                                          • 4i: Parsing (Useful Idioms)
                                                          • 4j: Parsing (Bases and Base Digits)
                                                          • 4k: Atom Printing
                                                          • 4l: Atom Parsing
                                                          • 4m: Formatting Functions
                                                          • 4n: Virtualization
                                                          • 4o: Molds
                                                          • 5a: Compiler Utilities
                                                          • 5b: Macro Expansion
                                                          • 5c: Compiler Backend & Prettyprinter
                                                          • 5d: Parser
                                                          • 5e: Molds and mold builders
                                                          • 5f: Profiling support
                                                          • Zuse

                                                            • Table of Contents
                                                            • 2d(1-5): To JSON, Wains
                                                            • 2d(6): From JSON
                                                            • 2d(7): From JSON (unit)
                                                            • 2e(2-3): Print & Parse JSON
                                                        • Nock

                                                          • Nock Definition
                                                          • Explanation
                                                          • Example
                                                          • Implementations
                                                          • Vere

                                                            • C Runtime System
                                                            • Land of Nouns
                                                            • API overview by prefix
                                                            • C in Urbit
                                                            • Writing Jets
                                                            • Cryptography
                                                            • Azimuth

                                                              • Overview
                                                              • Urbit HD Wallet
                                                              • Azimuth Data Flow
                                                              • Azimuth.eth
                                                              • Ecliptic.eth
                                                              • Advanced Azimuth Tools
                                                              • Life and Rift
                                                              • Layer 2

                                                                • Layer 2 Overview
                                                                • Layer 2 Actions
                                                                • Transaction Format
                                                                • Rollers
                                                                • Roller HTTP RPC-API
                                                                • Custom Roller Tutorial
                                                              • Glossary

                                                                • Ames
                                                                • Aqua
                                                                • Arm
                                                                • Arvo
                                                                • Atom
                                                                • Azimuth
                                                                • Battery
                                                                • Behn
                                                                • Bridge
                                                                • Censures
                                                                • Ceremony
                                                                • chat
                                                                • Claims
                                                                • Clay
                                                                • Comet
                                                                • Core
                                                                • Delegated Sending
                                                                • Desk
                                                                • Dill
                                                                • Document Proposal
                                                                • Dojo
                                                                • Door
                                                                • Ecliptic
                                                                • Event Log
                                                                • Eyre
                                                                • Ford
                                                                • Galaxy
                                                                • Gall
                                                                • Gate
                                                                • HD Wallet
                                                                • Hoon
                                                                • Invite Tree
                                                                • Iris
                                                                • Jael
                                                                • Jaque
                                                                • Keyfile
                                                                • Landscape
                                                                • Mark
                                                                • Moon
                                                                • Nock
                                                                • Noun
                                                                • OTA Updates
                                                                • Payload
                                                                • pH
                                                                • Pier
                                                                • Pill
                                                                • Planet
                                                                • Proxies
                                                                • Replay
                                                                • Factory Reset
                                                                • Naive rollups
                                                                • Sail/Udon
                                                                • Senate
                                                                • Ship
                                                                • ship.arvo.network
                                                                • Star
                                                                • |sync
                                                                • Trap
                                                                • Upgrade Proposal
                                                                • Vane
                                                                • Vere
                                                                • Voting
                                                                • Wallet-Generator
                                                                Urbit
                                                                • Introduction
                                                                • Development

                                                                  • Getting Started
                                                                  • Environment Setup
                                                                  • Grants Program
                                                                  • Project Repositories
                                                                  • Precepts
                                                                  • System Overview

                                                                    • Arvo
                                                                    • Hoon
                                                                    • Nock
                                                                    • Vere
                                                                    • Azimuth
                                                                    • Cryptography
                                                                    • Arvo

                                                                      • Overview
                                                                      • Ames

                                                                        • Overview
                                                                        • Cryptography
                                                                        • API Reference
                                                                        • Scry Reference
                                                                        • Data Types
                                                                        • Behn

                                                                          • Overview
                                                                          • API Reference
                                                                          • Scry Reference
                                                                          • Examples
                                                                          • Clay

                                                                            • Overview
                                                                            • Architecture
                                                                            • Using Clay
                                                                            • Data Types
                                                                            • Scry Reference
                                                                            • API Reference
                                                                            • Examples
                                                                            • Marks

                                                                              • Overview
                                                                              • Writing Marks
                                                                              • Using Marks
                                                                              • Examples
                                                                            • Dill

                                                                              • Overview
                                                                              • API Reference
                                                                              • Scry Reference
                                                                              • Data Types
                                                                              • Eyre

                                                                                • Overview
                                                                                • External API Reference
                                                                                • Internal API Reference
                                                                                • Scry Reference
                                                                                • Data Types
                                                                                • Guide
                                                                                • Ford

                                                                                  • Overview
                                                                                  • Gall

                                                                                    • Overview
                                                                                    • API Reference
                                                                                    • Data Types
                                                                                    • Iris

                                                                                      • Overview
                                                                                      • API Reference
                                                                                      • Data Types
                                                                                      • Example
                                                                                      • Jael

                                                                                        • Overview
                                                                                        • API Reference
                                                                                        • Scry Reference
                                                                                        • Data Types
                                                                                        • Examples
                                                                                        • Concepts

                                                                                          • Scries
                                                                                          • Subscriptions
                                                                                          • Tutorials

                                                                                            • Move Trace
                                                                                            • Reference

                                                                                              • Cryptography
                                                                                              • Filesystem Hierarchy
                                                                                            • Userspace

                                                                                              • Overview
                                                                                              • HTTP API Guide
                                                                                              • Gall Guide

                                                                                                • Introduction
                                                                                                • 1. Arvo
                                                                                                • 2. The Agent Core
                                                                                                • 3. Imports and Aliases
                                                                                                • 4. Lifecycle
                                                                                                • 5. Cards
                                                                                                • 6. Pokes
                                                                                                • 7. Structures and Marks
                                                                                                • 8. Subscriptions
                                                                                                • 9. Vanes
                                                                                                • 10. Scries
                                                                                                • 11. Failure
                                                                                                • 12. Next Steps
                                                                                                • Appendix: Types
                                                                                                • Full-Stack Walkthrough

                                                                                                  • 1. Introduction
                                                                                                  • 2. Types
                                                                                                  • 3. Agent
                                                                                                  • 4. JSON
                                                                                                  • 5. Marks
                                                                                                  • 6. Eyre
                                                                                                  • 7. React app setup
                                                                                                  • 8. React app logic
                                                                                                  • 9. Desk and glob
                                                                                                  • 10. Summary
                                                                                                  • Graph Store

                                                                                                    • Graph Store Overview
                                                                                                    • Data Structure Overview
                                                                                                    • Validator Walkthrough
                                                                                                    • Sample Application: Library
                                                                                                    • Graph Store - Reference
                                                                                                    • Advanced Info
                                                                                                    • Threads

                                                                                                      • Overview
                                                                                                      • HTTP API
                                                                                                      • Reference
                                                                                                      • Basics

                                                                                                        • Fundamentals
                                                                                                        • Bind
                                                                                                        • Input
                                                                                                        • Output
                                                                                                        • Summary
                                                                                                        • Gall

                                                                                                          • Start Thread
                                                                                                          • Take Result
                                                                                                          • Take Facts
                                                                                                          • Stop Thread
                                                                                                          • Poke Thread
                                                                                                          • Examples

                                                                                                            • Fetch JSON
                                                                                                            • Child Thread
                                                                                                            • Main-loop
                                                                                                            • Poke Agent
                                                                                                            • Scry
                                                                                                            • Take Fact
                                                                                                          • Distribution

                                                                                                            • Overview
                                                                                                            • Guide
                                                                                                            • Docket File
                                                                                                            • Glob
                                                                                                            • Dojo Tools
                                                                                                          • Hoon

                                                                                                            • Overview
                                                                                                            • Hoon School

                                                                                                              • 1.1 Introduction
                                                                                                              • 1.1.1 Walkthrough: List of Numbers
                                                                                                              • 1.2 Nouns
                                                                                                              • 1.3 Hoon Syntax
                                                                                                              • 1.3.1 Walkthrough: Conditionals
                                                                                                              • 1.4 Gates (Hoon Functions)
                                                                                                              • 1.4.1 Walkthrough: Recursion
                                                                                                              • 1.5 Lists
                                                                                                              • 1.5.1 Walkthrough: Fibonacci Sequence
                                                                                                              • 1.6 The Subject and Its Legs
                                                                                                              • 1.6.1 Walkthrough: Ackermann Function
                                                                                                              • 1.7 Arms and Cores
                                                                                                              • 1.7.1 Walkthrough: Caesar Cipher
                                                                                                              • 1.8 Doors
                                                                                                              • 1.8.1 Bank Account
                                                                                                              • 1.9 Generators
                                                                                                              • 2.1 Atoms, Auras, and Simple Cell Types
                                                                                                              • 2.2 Type Checking and Type Inference
                                                                                                              • 2.3 Structures and Complex Types
                                                                                                              • 2.3.1 Walkthrough: Libraries
                                                                                                              • 2.3.2 Molds
                                                                                                              • 2.4 Standard Library: Trees, Sets, and Maps
                                                                                                              • 2.5 Type Polymorphism
                                                                                                              • 2.5.1 Walkthrough: Iron Polymorphism and Wet Polymorphism
                                                                                                              • 2.5.2 Walkthrough: Lead Polymorphism
                                                                                                              • 2.6 Behn
                                                                                                              • 2.7 Gall
                                                                                                              • 2.7.1 Gall Walkthrough: Egg Timer
                                                                                                              • Guides

                                                                                                                • CLI apps
                                                                                                                • Parsing
                                                                                                                • JSON
                                                                                                                • Strings
                                                                                                                • Sail (HTML)
                                                                                                                • Writing Aqua Tests
                                                                                                                • Reference

                                                                                                                  • Cheat Sheet
                                                                                                                  • Irregular forms
                                                                                                                  • Hoon Errors
                                                                                                                  • Hoon Style Guide
                                                                                                                  • Basic Types
                                                                                                                  • Advanced Types
                                                                                                                  • Auras
                                                                                                                  • Runes

                                                                                                                    • Atoms and strings
                                                                                                                    • Nock . ('dot')
                                                                                                                    • Wild ! ('zap')
                                                                                                                    • Change Subject = ('tis')
                                                                                                                    • Conditionals ? ('wut')
                                                                                                                    • Cores | ('bar')
                                                                                                                    • Arms + ('lus')
                                                                                                                    • Cells : ('col')
                                                                                                                    • Calls % ('cen')
                                                                                                                    • Casts ^ ('ket')
                                                                                                                    • Structures $ ('buc')
                                                                                                                    • Make ; ('mic')
                                                                                                                    • Hints ~ ('sig')
                                                                                                                    • Imports / ('fas')
                                                                                                                    • Terminators -- and ==
                                                                                                                    • Limbs and wings

                                                                                                                      • Limbs
                                                                                                                      • Wings
                                                                                                                      • Standard library

                                                                                                                        • Table of Contents
                                                                                                                        • 1a: Basic Arithmetic
                                                                                                                        • 1b: Tree Addressing
                                                                                                                        • 1c: Molds and Mold-Builders
                                                                                                                        • 2a: Unit Logic
                                                                                                                        • 2b: List Logic
                                                                                                                        • 2c: Bit Arithmetic
                                                                                                                        • 2d: Bit Logic
                                                                                                                        • 2e: Insecure Hashing
                                                                                                                        • 2f: Noun Ordering
                                                                                                                        • 2g: Unsigned Powers
                                                                                                                        • 2h: Set Logic
                                                                                                                        • 2i: Map Logic
                                                                                                                        • 2j: Jar and Jug Logic
                                                                                                                        • 2k: Queue Logic
                                                                                                                        • 2l: Container from Container
                                                                                                                        • 2m: Container from Noun
                                                                                                                        • 2n: Functional Hacks
                                                                                                                        • 2o: Normalizing Containers
                                                                                                                        • 2p: Serialization
                                                                                                                        • 2q: Molds and Mold-Builders
                                                                                                                        • 3a: Modular and Signed Ints
                                                                                                                        • 3b: Floating Point
                                                                                                                        • 3c: Urbit Time
                                                                                                                        • 3d: SHA Hash Family
                                                                                                                        • 3e: AES encryption (Removed)
                                                                                                                        • 3f: Scrambling
                                                                                                                        • 3g: Molds and Mold-Builders
                                                                                                                        • 4a: Exotic Bases
                                                                                                                        • 4b: Text Processing
                                                                                                                        • 4c: Tank Printer
                                                                                                                        • 4d: Parsing (Tracing)
                                                                                                                        • 4e: Parsing (Combinators)
                                                                                                                        • 4f: Parsing (Rule-Builders)
                                                                                                                        • 4g: Parsing (Outside Caller)
                                                                                                                        • 4h: Parsing (ASCII Glyphs)
                                                                                                                        • 4i: Parsing (Useful Idioms)
                                                                                                                        • 4j: Parsing (Bases and Base Digits)
                                                                                                                        • 4k: Atom Printing
                                                                                                                        • 4l: Atom Parsing
                                                                                                                        • 4m: Formatting Functions
                                                                                                                        • 4n: Virtualization
                                                                                                                        • 4o: Molds
                                                                                                                        • 5a: Compiler Utilities
                                                                                                                        • 5b: Macro Expansion
                                                                                                                        • 5c: Compiler Backend & Prettyprinter
                                                                                                                        • 5d: Parser
                                                                                                                        • 5e: Molds and mold builders
                                                                                                                        • 5f: Profiling support
                                                                                                                        • Zuse

                                                                                                                          • Table of Contents
                                                                                                                          • 2d(1-5): To JSON, Wains
                                                                                                                          • 2d(6): From JSON
                                                                                                                          • 2d(7): From JSON (unit)
                                                                                                                          • 2e(2-3): Print & Parse JSON
                                                                                                                      • Nock

                                                                                                                        • Nock Definition
                                                                                                                        • Explanation
                                                                                                                        • Example
                                                                                                                        • Implementations
                                                                                                                        • Vere

                                                                                                                          • C Runtime System
                                                                                                                          • Land of Nouns
                                                                                                                          • API overview by prefix
                                                                                                                          • C in Urbit
                                                                                                                          • Writing Jets
                                                                                                                          • Cryptography
                                                                                                                          • Azimuth

                                                                                                                            • Overview
                                                                                                                            • Urbit HD Wallet
                                                                                                                            • Azimuth Data Flow
                                                                                                                            • Azimuth.eth
                                                                                                                            • Ecliptic.eth
                                                                                                                            • Advanced Azimuth Tools
                                                                                                                            • Life and Rift
                                                                                                                            • Layer 2

                                                                                                                              • Layer 2 Overview
                                                                                                                              • Layer 2 Actions
                                                                                                                              • Transaction Format
                                                                                                                              • Rollers
                                                                                                                              • Roller HTTP RPC-API
                                                                                                                              • Custom Roller Tutorial
                                                                                                                            • Glossary

                                                                                                                              • Ames
                                                                                                                              • Aqua
                                                                                                                              • Arm
                                                                                                                              • Arvo
                                                                                                                              • Atom
                                                                                                                              • Azimuth
                                                                                                                              • Battery
                                                                                                                              • Behn
                                                                                                                              • Bridge
                                                                                                                              • Censures
                                                                                                                              • Ceremony
                                                                                                                              • chat
                                                                                                                              • Claims
                                                                                                                              • Clay
                                                                                                                              • Comet
                                                                                                                              • Core
                                                                                                                              • Delegated Sending
                                                                                                                              • Desk
                                                                                                                              • Dill
                                                                                                                              • Document Proposal
                                                                                                                              • Dojo
                                                                                                                              • Door
                                                                                                                              • Ecliptic
                                                                                                                              • Event Log
                                                                                                                              • Eyre
                                                                                                                              • Ford
                                                                                                                              • Galaxy
                                                                                                                              • Gall
                                                                                                                              • Gate
                                                                                                                              • HD Wallet
                                                                                                                              • Hoon
                                                                                                                              • Invite Tree
                                                                                                                              • Iris
                                                                                                                              • Jael
                                                                                                                              • Jaque
                                                                                                                              • Keyfile
                                                                                                                              • Landscape
                                                                                                                              • Mark
                                                                                                                              • Moon
                                                                                                                              • Nock
                                                                                                                              • Noun
                                                                                                                              • OTA Updates
                                                                                                                              • Payload
                                                                                                                              • pH
                                                                                                                              • Pier
                                                                                                                              • Pill
                                                                                                                              • Planet
                                                                                                                              • Proxies
                                                                                                                              • Replay
                                                                                                                              • Factory Reset
                                                                                                                              • Naive rollups
                                                                                                                              • Sail/Udon
                                                                                                                              • Senate
                                                                                                                              • Ship
                                                                                                                              • ship.arvo.network
                                                                                                                              • Star
                                                                                                                              • |sync
                                                                                                                              • Trap
                                                                                                                              • Upgrade Proposal
                                                                                                                              • Vane
                                                                                                                              • Vere
                                                                                                                              • Voting
                                                                                                                              • Wallet-Generator
                                                                                                                              Urbit/Documentation/Userspace/Full-Stack Walkthrough

                                                                                                                              3. Agent

                                                                                                                              Now that we have our agent's types defined and have thought through its behavior, we can write the %journal agent itself.

                                                                                                                              Imports

                                                                                                                              /-  *journal
                                                                                                                              /+  default-agent, dbug, agentio
                                                                                                                              

                                                                                                                              We first import the /sur/journal.hoon file we previously created and expose its structures. We import the standard default-agent and dbug, and also an additional library called agentio.

                                                                                                                              Agentio contains a number of convenience functions to make common agent tasks simpler. For example, rather than writing out the full $cards when sending %facts to subscribers, we can call ++fact in agentio with the cage and paths and it will compose them for us. There are many more functions in agentio than we'll use here - you can have a look through the library in /base/lib/agentio.hoon to see what else it can do.

                                                                                                                              State and type core

                                                                                                                              |%
                                                                                                                              +$  versioned-state
                                                                                                                                  $%  state-0
                                                                                                                                  ==
                                                                                                                              +$  state-0  [%0 =journal =log]
                                                                                                                              +$  card  card:agent:gall
                                                                                                                              ++  j-orm  ((on id txt) gth)
                                                                                                                              ++  log-orm  ((on @ action) lth)
                                                                                                                              ++  unique-time
                                                                                                                                |=  [=time =log]
                                                                                                                                ^-  @
                                                                                                                                =/  unix-ms=@
                                                                                                                                  (unm:chrono:userlib time)
                                                                                                                                |-
                                                                                                                                ?.  (has:log-orm log unix-ms)
                                                                                                                                  unix-ms
                                                                                                                                $(time (add unix-ms 1))
                                                                                                                              --
                                                                                                                              

                                                                                                                              As we discussed in the previous section, our state will contain a $journal structure containing all our journal entries, and a $log structure containing the update log. These are both ordered maps, defined as ((mop id txt) gth) and ((mop @ action) lth) respectively. We can therefore define our versioned state as [%0 =journal =log], in the usual manner.

                                                                                                                              We've define $card for convenience as usual, and we've also added three more arms. The first two relate to our two ordered maps. If you'll recall, an ordinary map is called with the ++by door in the standard library, like so:

                                                                                                                              (~(get by foo) %bar)
                                                                                                                              

                                                                                                                              An ordered map uses the ++on gate in zuse.hoon rather than ++by, and its invocation is slightly different. It must first be setup in a similar manner to the mop type, by providing it the key/value molds and comparator gates. Once that's done, its individual functions can be called with the mop and arguments, like:

                                                                                                                              (get:((on @ud @ud) gth) foo %bar)
                                                                                                                              

                                                                                                                              This is quite a cumbersome expression to use every time we want to interact with our mop. To make it easier, we can store the ((on @ud @ud) gth) part in an arm, and then when we need to use it we can just do (get:arm-name foo %bar). In this case, we've done one each of our ordered maps like so:

                                                                                                                              ++  j-orm  ((on id txt) gth)
                                                                                                                              ++  log-orm  ((on @ action) lth)
                                                                                                                              

                                                                                                                              The last arm in our state definition core is ++unique-time. Since we'll use now.bowl to derive the timestamp for updates, we run into an issue if multiple pokes arrive in a single Arvo event. In that case, now.bowl would be the same for each poke, so they'd be given the same key and override each other in the mop. To avoid this, ++unique-time is just a simple recursive function that will increment the timestamp by one millisecond if the key already exists in the $log mop, ensuring all updates get unique timestamps and there are no collisions.

                                                                                                                              Agent core setup

                                                                                                                              %-  agent:dbug
                                                                                                                              =|  state-0
                                                                                                                              =*  state  -
                                                                                                                              ^-  agent:gall
                                                                                                                              |_  =bowl:gall
                                                                                                                              +*  this  .
                                                                                                                                  def   ~(. (default-agent this %|) bowl)
                                                                                                                                  io    ~(. agentio bowl)
                                                                                                                              ++  on-init  on-init:def
                                                                                                                              ++  on-save
                                                                                                                                ^-  vase
                                                                                                                                !>(state)
                                                                                                                              ::
                                                                                                                              ++  on-load
                                                                                                                                |=  old-vase=vase
                                                                                                                                ^-  (quip card _this)
                                                                                                                                `this(state !<(versioned-state old-vase))
                                                                                                                              ::
                                                                                                                              

                                                                                                                              Here we setup our agent core and define the three lifecycle arms. Since we only have a single state version at present, these are very simple functions. You'll notice in our +* arm, along with the usual this and def, we've also setup the agentio library we imported, giving it the bowl and an alias of io.

                                                                                                                              Pokes

                                                                                                                              ++  on-poke
                                                                                                                                |=  [=mark =vase]
                                                                                                                                ^-  (quip card _this)
                                                                                                                                |^
                                                                                                                                ?>  (team:title our.bowl src.bowl)
                                                                                                                                ?.  ?=(%journal-action mark)  (on-poke:def mark vase)
                                                                                                                                =/  now=@  (unique-time now.bowl log)
                                                                                                                                =/  act  !<(action vase)
                                                                                                                                =.  state  (poke-action act)
                                                                                                                                :_  this(log (put:log-orm log now act))
                                                                                                                                ~[(fact:io journal-update+!>(`update`[now act]) ~[/updates])]
                                                                                                                                ::
                                                                                                                                ++  poke-action
                                                                                                                                  |=  act=action
                                                                                                                                  ^-  _state
                                                                                                                                  ?-    -.act
                                                                                                                                      %add
                                                                                                                                    ?<  (has:j-orm journal id.act)
                                                                                                                                    state(journal (put:j-orm journal id.act txt.act))
                                                                                                                                  ::
                                                                                                                                      %edit
                                                                                                                                    ?>  (has:j-orm journal id.act)
                                                                                                                                    state(journal (put:j-orm journal id.act txt.act))
                                                                                                                                  ::
                                                                                                                                      %del
                                                                                                                                    ?>  (has:j-orm journal id.act)
                                                                                                                                    state(journal +:(del:j-orm journal id.act))
                                                                                                                                  ==
                                                                                                                                --
                                                                                                                              ::
                                                                                                                              

                                                                                                                              Here we have our ++on-poke arm, where we handle $actions. Since our %journal agent is intended for local use only, we make sure only our ship or our moons may perform actions with:

                                                                                                                              ?>  (team:title our.bowl src.bowl)
                                                                                                                              

                                                                                                                              We haven't yet written our mark files, but our mark for $actions will be %journal-action, so we make sure that's what we've received and if not, call ++on-poke:def to crash with an error message. We make sure the the timestamps are unique with our ++unique-time function described earlier, and then we extract the poke's vase to an $action structure and call ++poke-action to handle it. We've made ++on-poke a door with a separate ++poke-action arm to make the logic a little simpler, but in principle we could have had it all directly inside the main ++poke-action gate, or even separated it out into a helper core below.

                                                                                                                              The logic in ++poke-action is very simple, with three cases for each of the possible $actions:

                                                                                                                              • %add - Add a new journal entry. We check it doesn't already exist with ++has:j-orm, and then add it to our $journal with ++put:j-orm.

                                                                                                                              • %edit - Edit an existing journal entry. We make sure it does exist with ++has:j-orm, and then override the old entry with the new one using ++put:j-orm again.

                                                                                                                              • %del - Delete an existing journal entry. We make sure it exists again with ++has:j-orm, and then use ++del:j-orm to delete it from our $journal mop.

                                                                                                                              Back in the main part of ++on-poke, ++poke-action updates the state with the new $journal, then we proceed to:

                                                                                                                              :_  this(log (put:log-orm log now act))
                                                                                                                              ~[(fact:io journal-update+!>(`update`[now act]) ~[/updates])]
                                                                                                                              

                                                                                                                              We add the timestamp to the action, converting it to a logged update. We add it to the $log update log using ++put:log-orm, and also send the logged update out to subscribers on the /updates subscription path. We haven't written our mark files yet, but %journal-update is the mark we'll use for $updates, so we pack the $update in a vase and add the mark to make it a $cage. Notice we're using the ++fact function in agentio (which we aliased as io) rather than manually composing the %fact.

                                                                                                                              Subscriptions

                                                                                                                              ++  on-watch
                                                                                                                                |=  =path
                                                                                                                                ^-  (quip card _this)
                                                                                                                                ?>  (team:title our.bowl src.bowl)
                                                                                                                                ?+  path  (on-watch:def path)
                                                                                                                                  [%updates ~]  `this
                                                                                                                                ==
                                                                                                                              ::
                                                                                                                              

                                                                                                                              Our subscription logic is extremely simple - we just have a single /updates path, which the front-end or other local agents may subscribe to. All updates get sent out on this path. We enforce local-only with the team:title check.

                                                                                                                              We could have had our ++on-watch arm send out some initial state to new subscribers, but for our front-end we'll instead fetch the initial state separately with a scry. This just makes it slightly easier if our front-end needs to resubscribe at some point - it'll already have some state in that case so we don't want it to get sent again.

                                                                                                                              Scry Endpoints

                                                                                                                              ++  on-peek
                                                                                                                                |=  =path
                                                                                                                                ^-  (unit (unit cage))
                                                                                                                                ?>  (team:title our.bowl src.bowl)
                                                                                                                                =/  now=@  (unm:chrono:userlib now.bowl)
                                                                                                                                ?+    path  (on-peek:def path)
                                                                                                                                    [%x %entries *]
                                                                                                                                  ?+    t.t.path  (on-peek:def path)
                                                                                                                                      [%all ~]
                                                                                                                                    :^  ~  ~  %journal-update
                                                                                                                                    !>  ^-  update
                                                                                                                                    [now %jrnl (tap:j-orm journal)]
                                                                                                                                  ::
                                                                                                                                      [%before @ @ ~]
                                                                                                                                    =/  before=@  (rash i.t.t.t.path dem)
                                                                                                                                    =/  max=@  (rash i.t.t.t.t.path dem)
                                                                                                                                    :^  ~  ~  %journal-update
                                                                                                                                    !>  ^-  update
                                                                                                                                    [now %jrnl (tab:j-orm journal `before max)]
                                                                                                                                  ::
                                                                                                                                      [%between @ @ ~]
                                                                                                                                    =/  start=@
                                                                                                                                      =+  (rash i.t.t.t.path dem)
                                                                                                                                      ?:(=(0 -) - (sub - 1))
                                                                                                                                    =/  end=@  (add 1 (rash i.t.t.t.t.path dem))
                                                                                                                                    :^  ~  ~  %journal-update
                                                                                                                                    !>  ^-  update
                                                                                                                                    [now %jrnl (tap:j-orm (lot:j-orm journal `end `start))]
                                                                                                                                  ==
                                                                                                                                ::
                                                                                                                                    [%x %updates *]
                                                                                                                                  ?+    t.t.path  (on-peek:def path)
                                                                                                                                      [%all ~]
                                                                                                                                    :^  ~  ~  %journal-update
                                                                                                                                    !>  ^-  update
                                                                                                                                    [now %logs (tap:log-orm log)]
                                                                                                                                  ::
                                                                                                                                      [%since @ ~]
                                                                                                                                    =/  since=@  (rash i.t.t.t.path dem)
                                                                                                                                    :^  ~  ~  %journal-update
                                                                                                                                    !>  ^-  update
                                                                                                                                    [now %logs (tap:log-orm (lot:log-orm log `since ~))]
                                                                                                                                  ==
                                                                                                                                ==
                                                                                                                              ::
                                                                                                                              

                                                                                                                              Here we have our ++on-peek arm. The scry endpoints we've defined are divided into two parts: querying the update $log and retrieving entries from the $journal. Each end-point is as follows:

                                                                                                                              • /x/entries/all - Retrieve all entries in the $journal. Our front-end will use lazy-loading and only get a few at a time, so it won't use this. It's nice to have it though, in case other agents want to get that data.

                                                                                                                              • /x/entries/before/[before]/[max] - Retrieve at most [max] entries older than the entry on [before] date. This is so our lazy-loading front-end can progressively load more as the user scrolls down the page. The Javascript front-end will format numbers without dot separators, so the path will look like /x/entries/before/1648051573109/10. We therefore have to use the ++dem parsing rule in a ++rash parser to convert it to an ordinary atom. We then use the ++tap:log-orm mop function to retrieve the requested range as a list and return it as an $update with a %journal-update mark.

                                                                                                                              • /x/entries/between/[start]/[end] - Retrieve all journal entries between two dates. This is so our front-end can have a search function, where the user can enter a start and end date and get all the entries in between. The ++lot:j-orm mop function returns the subset of a mop between the two given keys as a mop, and then we call ++tap:j-orm to convert it to a list. The ++lot:j-orm function excludes the start and end values, so we subtract 1 from the start and add 1 to the end to make sure it includes the full range.

                                                                                                                              • /x/updates/all - Retrieve the entire update $log. Our front-end won't use this but it might be useful for other agents, so we've included it here.

                                                                                                                              • /x/updates/since/[since] - Retrieve all $updates that have happened since the specified timestamp, if any. This is so our front-end (or another agent) can resynchronize its state in the event its subscription is interrupted, without having to fetch everything from scratch again.

                                                                                                                              We don't use any of the other agent arms, so the remainder have all been passed to default-agent for handling:

                                                                                                                              ++  on-leave  on-leave:def
                                                                                                                              ++  on-agent  on-agent:def
                                                                                                                              ++  on-arvo   on-arvo:def
                                                                                                                              ++  on-fail   on-fail:def
                                                                                                                              --
                                                                                                                              

                                                                                                                              The full agent source can be viewed here.

                                                                                                                              Resources

                                                                                                                              • The Gall Guide - The Gall Guide covers all aspects of writing Gall agents in detail.

                                                                                                                              • Ordered map functions in zuse.hoon - This section of zuse.hoon contains all the functions for working with mops, and is well commented.

                                                                                                                              • /lib/agentio.hoon - The agentio library in the %base desk contains a large number of useful functions which making writing Gall agents easier.

                                                                                                                              <-

                                                                                                                              2. Types

                                                                                                                              4. JSON

                                                                                                                              ->

                                                                                                                              Edit this page on GitHub